Click here to Skip to main content
16,004,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hoi people,
457 / 5.000
I have been struggling for a few weeks to add an item to a ListView.
I have a mainframe with contain a ListView and buttons on top.
When I press a button at the top, I call a UserControl where I create the class button(AddButton).

so I have a mainframe => UserControl => Addbuttons class

When I press the Usercontrol on one of the buttons, I gather some text and have to pass it to the ListView on the mainframe.

This is what i got (mainframe)

C#
public partial class frmHoofd : Form
 {
     private ContrlVoorgerechten u1;

     public frmHoofd()
     {
         InitializeComponent();
     }      

     public void UpdatingListView()
     {
       //  MessageBox.Show("need to update");
     }

     private void btVoorgerechten_Click(object sender, EventArgs e)
     {
           //calling the Usercontrole
           u1 = new ContrlVoorgerechten() ;
           //u1.Dock = DockStyle.Fill;
           u1.Width = 750;
           u1.Height = 477;
           u1.Location = new Point(430, 165);

           this.Controls.Add(u1);
     }

 }


i call the UserControl and makes some buttons

C#
public partial class ContrlVoorgerechten : UserControl
{

    public ContrlVoorgerechten()
 {
     InitializeComponent();
     AddButton addButton = new AddButton();
     int twintig = 17;//eerste rij
     int honderdvijf = 135;//tweede rij
     int tweehonderdvijf = 250; // derde rij
     addButton.New( this, "1.Tom-Yam", 2, twintig, "Images.jpg", "8");
     addButton.New(this, "2.Tom-Kha", 165, twintig, "Images.jpg","8");
     addButton.New(this, "3.Wantan", 325, twintig, "Images.jpg", "8");
}


in the class buttons, is gives the style.
Its here i click the button and pass it to my mainframe where the ListView is located.

C#
internal class AddButton
 {
     //public delegate void MyDelegate(Bestelling _bestelling);
     public EventHandler<Bestelling> ClickEvent;

     public void New(UserControl user, String text, int Location_X, int Location_Y, String stringPath, string prijs)
     {
         System.Windows.Forms.Button button = new System.Windows.Forms.Button
         {
             Text = text,

             Location = new Point(Location_X, Location_Y),
             Name = prijs,

             TextAlign = ContentAlignment.BottomCenter,
             Font = new Font("Microsoft Sans Serif", 11),
             BackColor = Color.White,
             Width = 150,
             Height = 100,
             FlatStyle = FlatStyle.Flat,
             Image = System.Drawing.Image.FromFile(stringPath)
         };
         button.Click += BTNclick;
         user.Controls.Add(button);
         user.CreateControl();

         // button.Click += new System.EventHandler(OnClick);
     }

     public void BTNclick(object sender, EventArgs e)
     {

     System.Windows.Forms.Button clickedbtn = sender as System.Windows.Forms.Button;

         Bestelling orders = new Bestelling();

          orders.setDiscription(clickedbtn.Text); //6.Tod Monpla
          orders.setPrice(Convert.ToInt32(clickedbtn.Name));
          orders.setAantal(1);

         //FrmHoofd frm = new frmHoofd();
         //ClickEvent.Invoke(this, bestelling);

        /* clDelegates _delegate = new clDelegates(this);
         _delegate.UpdateGrid(bestelling);*/

     }


I have tried many things but non of one is working.
I can pass the parameters through the Mainframe and call the function Updating ListView but they ListView is not updated. Nothing appears.

Pleae somebody to help.

What I have tried:

I have tried many things but non of one is working.
I can pass the parameters through the Mainframe and call the function Updating ListView but they ListView is not updated. Nothing appears.

Pleae somebody to help.
Posted
Updated 20-Aug-24 13:11pm
v4

At a guess - and without being able to run your code that's all it can be - you don't appear to be updating a ListView anywhere in your code, unless the Bestelling class has something to do with it. And that new instance you create in your Click event handler doesn't appear to be used at all - you create it, do three things with it, and then discard it without obviously storing it anywhere.

We can;t from that say "do this" and it'll fix it - it needs your code running in order to start working out what the problem is. So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line of each function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. In particular, breakpoint the code that you think should be updating the ListView.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
jan leung 21-Aug-24 3:42am    
Thank you for your replay.
The reason why i didn't fill up my code is be cause i have tried many things and don't know what to use.
So please can you tell me what i should use? EventHandler, delegates, Action, ....
OriginalGriff 21-Aug-24 3:51am    
What did the debugger show you is actually happening with your existing code?
jan leung 21-Aug-24 5:10am    
Nothing no errors.
It just didn't update the ListView.
OriginalGriff 21-Aug-24 5:29am    
"Nothing no errors"
A debugger doesn't run and go "It's there! That line is your problem!" for you.
It lets you look at exactly what your code is doing while it runs so you can discover where things happen that you didn't expect, or something changes in a way you didn't expect. You wrote the code, you know what you expect it to do at each step. The debugger doesn't: it has no idea what your project ios meant to do, much less any particular method or line of code!

So run your code in the debugger and look at exactly what it happening: did that method get executed? If not, why not? If it did, did it do what you expected it to? If it didn't, in what way did what it did do differ from your exp[ectations? It's a process, not a solution - like making a cup of coffee is a process: if my bean to cup machine doesn't doesn't produce a glass cup or magic black liquid, what did it produce? If it just sat there and did nothing, then the problem may be it isn't turned on, or the mains is disconnected, or there is a power cut. If it produces green goo then some child if going to regret it's actions ...
The debugger enables you to make informed guesses as to why something failed, and then test that hypothesis by looking at the actual facts. And then change the code to see if that solves the problem.
jan leung 21-Aug-24 5:14am    
For one reason i can't acces the ListView.
I agree with OriginalGriff.
But one suggestion : in my opinion the method "BTNClick" should be placed where the UserControl is instanted (the Form ?) - there you have direct access to the ListView I suppose. Actually you do nothing with "orders".
 
Share this answer
 
Comments
jan leung 21-Aug-24 4:00am    
Thank you for your replay.
The reason why i didn't fill up my code is be cause i have tried many things and don't know what to use.
Yes the problem i can't have direct acces to the ListView. But how i need to do that?
What do i need to use?
How i put "BTNClick" in the usercontrol?
Can i do it with => button.Click += new System.EventHandler(OnClick);
Ralf Meier 21-Aug-24 17:12pm    
I suppose that my suggestion goes the same way as mentioned by Pete in Solution3.
You should create your own Event inside the UserControl and raise this Event with yout Button-Click. But the Sender in this case should not be the UserControl - the Sender has to be the Button and so you could get outside the UserControl all the information you need ...
Okay, it's taken me a little bit to get my head around your code but I think I see what you are trying to do here. The big problem that you have, from the code you posted, is that you have hooked up the button click to an event handler, but you don't actually raise an event from your user control with the details that you need. So, you need the user form to hook up to your ClickEvent (which you have commented out), but you need to do that without creating a new instance of the form. So, in your form, you need something like:
C#
control.ClickEvent += ClickEventHandler;
//... other code...
private void ClickEventHandler(object sender, Bestselling bestselling)
{
  // Update the listview here.
}
 
Share this answer
 
Comments
jan leung 21-Aug-24 7:12am    
thx Pete,
I'm going to try this out.
If i put the control.ClickEvent ... in my mainform, then i should be able to acces my ListView?


sorry for my language.... I'm from Europe
Pete O'Hanlon 21-Aug-24 7:32am    
If your ListView is in your main form, then yes. And don't worry about your English, it was good enough to get the question across.
jan leung 23-Aug-24 15:00pm    
Thank you all for your help.
I finally succeed ....
Pete O'Hanlon 23-Aug-24 15:11pm    
Congratulations.
Mainform i put the function UpdatingListView

C#
public void UpdatingListView(Bestelling orders)
{
    ListViewItem lvi = new ListViewItem(orders.getDiscription());

    lvi.SubItems.Add(Convert.ToString(orders.getvalue()));
    lvi.SubItems.Add(Convert.ToString(orders.getPrice())+" €");

    lstBesteld.Items.Add(lvi);
}


In the UserControl, i pass the mainform.
In my function where is set buttons, i have a clickEvent which passing an object

C#
<pre> public ContrlVoorgerechten(frmHoofd frm)
 {
     InitializeComponent();
     this.frm = frm;

     AddButton addButton = new AddButton();
     addButton.ClickEvent += (s,orders) =>
     {
         frm.UpdatingListView(orders);

     };

     int twintig = 17;//eerste rij
     int honderdvijf = 135;//tweede rij
     int tweehonderdvijf = 250; // derde rij
     addButton.New(this, "1.Tom-Yam", 2, twintig, "Images\\tom-yam.jpg", "8");
     addButton.New(this, "2.Tom-Kha", 165, twintig, "Images\\tom-kha.jpg", "8");
     addButton.New(this, "3.Wantan", 325, twintig, "Images\\legefoto.jpg", "8");



and the class where i create the buttonsi call the
EventHandler(BTNclick);

<pre lang="C#">
<pre> 
public EventHandler<Bestelling> ClickEvent;

  public void New(UserControl user, String text, int Location_X, int Location_Y, String stringPath, string prijs)
  {
      System.Windows.Forms.Button button = new System.Windows.Forms.Button
      {
          Text = text,

          Location = new Point(Location_X, Location_Y),
      
          Name = prijs,

          TextAlign = ContentAlignment.BottomCenter,
          Font = new Font("Microsoft Sans Serif", 11),
          BackColor = Color.White,
          Width = 150,
          Height = 100,
          FlatStyle = FlatStyle.Flat,
          Image = System.Drawing.Image.FromFile(stringPath)
      };
   
      button.Click += new System.EventHandler(BTNclick);
     
      user.Controls.Add(button);
   }


  public void BTNclick(object sender, EventArgs e)
  {

      System.Windows.Forms.Button clickedbtn = sender as System.Windows.Forms.Button;

      Bestelling orders = new Bestelling();

       orders.setDiscription(clickedbtn.Text); //6.Tod Monpla
       orders.setPrice(Convert.ToInt32(clickedbtn.Name));
       orders.setAantal(1);

       ClickEvent.Invoke(this,orders);

  }



Thank you and hope somebody else can learn from it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900