Adding text to a listbox

I am trying to add text to a listbox by using a string type variable. During debug I noticed that the variable had the text I wished to add to the listbox, but the listBox1.Items.Add(s_Text); didnt' do anything. Is there a different way your supposed to do this


Answer this question

Adding text to a listbox

  • pgoel6uc

    I wasn't talking about a Textbox control, but a string variable. The variable is recieving a value from an object successfully, but will not add it to a listbox for whatever reason.

  • kostrin

    it should do - what happens when you add it Any object you can add into the listbox as the Add() method takes in an object

  • patag2001

    send over the entire project (email in my profile, just click the name) and ill be sure to post the solution

  • SteveMo

    When I try to add it nothing happens, no Exception error or anything.

  • KitGreen

    Did you try to add a string value like "hello" and see what happends. Maybe you are adding values in wrong listbox. Play with your code when you have such unpredictable situation. Maybe your listbox don't shows items, or lisbox is disabled.

  • Vaish

    ok the problem is simple. The user creates another main form instance in the other form "Add Items" and adds items to this new main form instance - this will not work. It will continue to operate but since the original existing main form is "alive", you are not dealing/handling with it but creating a new instance (another instance) of the main form.

    You need to reference the main form into this "Add Items" form and add the items as you have done in the code snippet:

    frmMain mainForm = new frmMain(); //this needs to be taken out

    MessageBox.Show("" + i_currentItem);

    MessageBox.Show("Item Name: " + newItem.Get_itemName());

    mainForm.Set_item(newItem, mainForm.Get_numItems());

    So, read the thread below on how to pass a reference of an object, your main form in this case, to this "Add Items" form, then simply access it (access the main form) in the "Add Items" form to add the items to the main form:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=729974&SiteID=1 (around the 2nd/3rd response of mine with a code snippet)



  • Tom McAnnally

    should be:

    this.listBox1.Items.Add(theTextBox.Text);

    textboxes are controls, to get or set its textual property, use the "Text" property of the textbox control and most text based controls (combobox, label, textbox etc...)



  • Steve Hempen

    Thanks a bunch! Everything works fine now.

  • Adding text to a listbox