• 0

ListBox clearing


Question

Ok, I want to clear just selected items in my listbox in C#. Everytime I try to do it thought it throws an Enumeration error. Here's my code:

foreach (string item in lstFiles.SelectedItems)
{
lstFiles.Items.Remove(item);
}

What do I do??

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

You're going to have to loop backwards and delete them like that. In a foreach you cant modify the collection. :)

Dan

EDIT: whipped up some code for ya:

?	for (int i = listBox1.SelectedItems.Count - 1; i > -1; i--)
 ?	{
 ? ?listBox1.Items.Remove(listBox1.SelectedItems[i]);
 ?	}

Edited by dannyres
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.