locked
How to Remove ListItem from a ListBox RRS feed

  • Question

  • Hi friends

    when i try to remove ListBox item(ListItem) its giving error

    error  :

    List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

    i have only ListItem objects in my ListBox and my code as follows

    foreach (ListItem removeitem in listBox1.SelectedItems)

    {

    listBox1.Items.Remove(removeitem);

    }

    any help pls,

    Regards

    Ranadheer.

    Thursday, January 18, 2007 10:10 AM

Answers

  • Hi,

    You cannot remove the items in this way, since foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

    You can do the same thing by while loop or by for loop anyway.

    For example:

                while(listBox1.SelectedItems.Count!=0)

                {

                    listBox1.Items.Remove(listBox1.SelectedItems[0]);

                }

    Thank you

    Friday, January 19, 2007 8:47 AM

All replies

  • Try this

    int SelItemCount = listBox1.SelectedItems.Count;

    for (int i = 0; i < SelItemCount; i++)
    {
       listBox1.Items.Remove(listBox1.SelectedItem);
    }

    • Proposed as answer by DaniloTec Wednesday, April 27, 2011 9:20 PM
    Thursday, January 18, 2007 11:21 AM
  • That will remove the first N items (where N is the number of selected items) rather than actually removing the selected items (which is what I think the OP is trying to achieve).

    Perhaps the thing to do is to extract the selected item references into a separate array, and then iterate through THAT array to delete the items.

    Thursday, January 18, 2007 11:25 AM
  •  ranadheer mac wrote:

    Hi friends

    when i try to remove ListBox item(ListItem) its giving error

    error  :

    List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

    i have only ListItem objects in my ListBox and my code as follows

    foreach (ListItem removeitem in listBox1.SelectedItems)

    {

    listBox1.Items.Remove(removeitem);

    }

    any help pls,

    Regards

    Ranadheer.

    You are using foreach loop to remove items replace your loop to be for or any other loop but not the foreach loop. This problem is caused because when you use foreach  loop for safety it makes a check on each iteration that array is not modified so removing it in itself will definitely cause a problem.

    Use any other loop than foreach and all will be smooth.

    I hope this will solve your problem.

    Best Regards

    Rizwan aka RizwanSharp

    Thursday, January 18, 2007 3:50 PM
  • Hi,

    You cannot remove the items in this way, since foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

    You can do the same thing by while loop or by for loop anyway.

    For example:

                while(listBox1.SelectedItems.Count!=0)

                {

                    listBox1.Items.Remove(listBox1.SelectedItems[0]);

                }

    Thank you

    Friday, January 19, 2007 8:47 AM
  • Friday, January 19, 2007 12:45 PM
  • Hi Friend,

    Herewith I have attached the following Code for removing selected item(s) from the Listbox.

    Note: Whatever selected Item's Index value returned to colSelected variable.(So I can Iterate)

    Dim Li As Int32

    Dim colSelected = ListBox1.SelectedIndices

    For Li = colSelected.Count - 1 To 0 Step -1

    ListBox1.Items.RemoveAt(colSelected(Li))

    Next

    With Regards,

    Pandian S

     

    Sunday, January 21, 2007 8:06 AM
  • int SelItemCount = listBox1.SelectedItems.Count;

    for (int i = 0; i < SelItemCount; i++)
    {
       listBox1.Items.Remove(listBox1.SelectedItem);
    }

     code from DaniloTec works.

    Monday, October 29, 2012 4:17 AM
  • Hi brother,

    It works.. Thanks 4 Ur code.. ^_^

    foreach (ListItem removeitem in listBox1.SelectedItems)
    {
    listBox1.Items.Remove(removeitem);
    }

    Best Regards

    VoDa Kar....

    Wednesday, June 12, 2013 8:00 AM
  • Hi,

    You cannot remove the items in this way, since foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

    You can do the same thing by while loop or by for loop anyway.

    For example:

                while(listBox1.SelectedItems.Count!=0)

                {

                    listBox1.Items.Remove(listBox1.SelectedItems[0]);

                }

    Thank you

    Thank you for your response, it helped me solve an issue I was having using a foreach statement to remove selected items in a listbox. 

    You say that using a foreach to change the contents of the collection causes unpredictable side effects and it did in my case. Can you elaborate why it does but using a while loop does not? I can't seem to wrap my head around it. 


    Thursday, September 18, 2014 8:28 PM
  • Brilliant Figo , I wasted so much time trying to delete a single selected item (multiples not an option in my app).

    Your method never occurred to me because it will always be 0 or 1 selected!

    I checked for selectedindex = -1 before removing the selected item in many ways!

    No errors but did not do a thing!

    Thank you , your method works perfectly without checking if any are selected!


    Monday, October 7, 2019 10:53 AM
  • BTW I was using VBA so a very small adaption is needed for beginners.

      While ListBox3.SelectedItems.Count <> 0
         ListBox3.Items.Remove(ListBox3.SelectedItems(0))
       End While

    Monday, October 7, 2019 10:59 AM