Listbox deleting items
-
I seem to be encountering difficulty while trying to delete an item from a listbox. The user selects an item from the listbox, then clicks a delete button. The button code is as follows:
try { listBox1.Items.Remove(listBox1.SelectedItem); } catch(IndexOutOfRangeException ex) { MessageBox.Show("Could not delete the selected item", "Error", MessageBoxButtons.OK); }
Now, if I select the first item in the list, and press delete, the exception IndexOutOfRange occurs, however the item is then successfully deleted. If I select any other item in the list and try to delete it, it works fine, no exception Why does deleting the first item cause an exception -
I seem to be encountering difficulty while trying to delete an item from a listbox. The user selects an item from the listbox, then clicks a delete button. The button code is as follows:
try { listBox1.Items.Remove(listBox1.SelectedItem); } catch(IndexOutOfRangeException ex) { MessageBox.Show("Could not delete the selected item", "Error", MessageBoxButtons.OK); }
Now, if I select the first item in the list, and press delete, the exception IndexOutOfRange occurs, however the item is then successfully deleted. If I select any other item in the list and try to delete it, it works fine, no exception Why does deleting the first item cause an exception -
I test your code and there is no problem with that. It works properly for all items. Whats your error message? Mazy No sig. available now.
i just get an "IndexOutOfRangeException" occuring. Im using Borland C# if thats any help?
-
i just get an "IndexOutOfRangeException" occuring. Im using Borland C# if thats any help?
Gavin Coates wrote: i just get an "IndexOutOfRangeException" occuring Every exeptions have a
Message
property, which is string contained a error string. Gavin Coates wrote: Borland C# Oh, I don't know about that. I haven't seen anybody use it here. Mazy No sig. available now. -
Gavin Coates wrote: i just get an "IndexOutOfRangeException" occuring Every exeptions have a
Message
property, which is string contained a error string. Gavin Coates wrote: Borland C# Oh, I don't know about that. I haven't seen anybody use it here. Mazy No sig. available now.error is: "Index was outside the bounds of the array"
-
error is: "Index was outside the bounds of the array"
Try private void btnDelTheListBoxItem_Click(object sender, System.EventArgs e) { if(TheListBox.SelectedItem != null) { for(int i = 0; i < TheListBox.Items.Count; i++) { if(TheListBox.SelectedItem.Equals(TheListBox.Items[i])) { TheListBox.Items.RemoveAt(i); i = TheListBox.Items.Count; } } } } This will work
-
Try private void btnDelTheListBoxItem_Click(object sender, System.EventArgs e) { if(TheListBox.SelectedItem != null) { for(int i = 0; i < TheListBox.Items.Count; i++) { if(TheListBox.SelectedItem.Equals(TheListBox.Items[i])) { TheListBox.Items.RemoveAt(i); i = TheListBox.Items.Count; } } } } This will work
ggthanks thomasa. I tried the code above, but again i get exactly the same results! I can delete any item in the listbox no problems, but when i try to delete the first item, it deletes, but causes an IndexOutOfRangeException!! I tried creating a whole new project with just a listbox, textbox add and delete button, and it worked perfectly, no errors at all. It just causes problems in my current project!!! Please help!
-
ggthanks thomasa. I tried the code above, but again i get exactly the same results! I can delete any item in the listbox no problems, but when i try to delete the first item, it deletes, but causes an IndexOutOfRangeException!! I tried creating a whole new project with just a listbox, textbox add and delete button, and it worked perfectly, no errors at all. It just causes problems in my current project!!! Please help!