How can I pass a class value to a Listbox

Asked

Viewed 754 times

1

I am involved in a small personal project, composed of some classes and many photons. I need to send a value that is obtained when I access a certain button, to the listbox that is in one of these Forms. For this I chose to build a method called beverageclick with the following code

 public void beverageclick(object sender, EventArgs e)
        {
            Button b = (Button)sender;//button sender

        string value = (string)b.Tag;// value = b.tag (tag of sended button tag = value (value of  AddBeverageDrinkstoTabbedPanel) method)
        beverages.Add(value);
        //get value


        //MessageBox.Show(value);
        ListBox lstbox = new ListBox();
        lstbox.Items.Add(value);



        } 

How can I change the above code so that when accessing a button, the value of that button (text) is inserted in the listbox.

  • 2

    Apparently the problem here is that you are creating a new instance of ListBox that is not the one you included in your form.

1 answer

2

You are creating a whole Listbox made this button click. Add this value in a previously created listbox, take ListBox lstbox = new ListBox();. But if you want to create a Listbox every time the event is triggered don’t forget to put this listbox in a panel or something like that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.