Removing or editing items in a listbox

I want to write a function that will remove or edit the selected item in a listbox. I tried all sorts of things but I couldn't figure it out. Can someone help me please
Thanks :)



Answer this question

Removing or editing items in a listbox

  • Aaron Sulwer

    you can also use this to remove the item:

    if (this.theListBox.SelectedIndex > -1)

    {

    this.theListBox.Items.Remove(this.theListBox.SelectedIndex);

    }

    to edit, try....

    if (this.theListBox.SelectedIndex > -1)

    {

    this.theListBox.Items[this.theListBox.SelectedIndex] = "new value";

    }



  • gr8mind

    How about this:

    string itemToEdit = (string)listBox1.SelectedItem;

    //change the item here

    itemToEdit = itemToEdit + "xx"; //for example

    listBox1.Items[listBox1.SelectedIndex ] = itemToEdit;


  • TaYeB

    That works great :D but how should I go about editing the selected item.


  • HsiaoI

    Try this:

    listBox1.Items.Remove(listBox1.SelectedItem);


  • Removing or editing items in a listbox