"Bilo" wrote...
private void button2_Click(object sender, System.EventArgs e)
{
foreach (string filename in listBox1.SelectedItems)
listBox1.Items.Remove(filename);
}
The error actually spells it out:
System.InvalidOperationException:
The list that this enumerator is bound to
has been modified. An enumerator can only
be used if the list doesn't change.
When you Remove an item from the ListBox, you also affect the "contents" of
its SelectedItems, which makes it impossible to continue the iteration, as
the iteration is *based* on it.
You could try to do something like this instead:
string[] sa = new string[listBox1.SelectedItems.Count];
listBox1.SelectedItems.CopyTo(sa,0);
foreach (string filename in sa)
{
listBox1.Items.Remove(filename);
}
// Bjorn A