By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

listBox1.Items.Remove help

P: n/a
I dont know what i am doing false.
the code :

private void button2_Click(object sender, System.EventArgs e)
{
foreach (string filename in listBox1.SelectedItems)
listBox1.Items.Remove(filename);
}
gives me the error :

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.
at
System.Windows.Forms.EntryEnumerator.System.Collec tions.IEnumerator.MoveNext
()

Anyone can help?

Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Bilo wrote:

I dont know what i am doing false.
the code :

private void button2_Click(object sender, System.EventArgs e)
{
foreach (string filename in listBox1.SelectedItems)
listBox1.Items.Remove(filename);
}
gives me the error :

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.
at
System.Windows.Forms.EntryEnumerator.System.Collec tions.IEnumerator.MoveNext
()

Anyone can help?


You can't remove items from the listbox while you are iterating over that very
listbox. The problem is because it becomes difficult to maintain consistency
if items within the list are being removed.

What you need to do is to create an intermediate structure to hold the items to
be removed, then remove those items:

ArrayList files = new ArrayList();
foreach (string filename in listBox1.SelectedItems)
files.Add(filename);
foreach (string filename in files)
listBox1.Items.Remove(filename);
Nov 16 '05 #2

P: n/a
"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
Nov 16 '05 #3

P: n/a
Ahh first store the items.
Thx you.

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> schrieb im Newsbeitrag
news:Ox**************@TK2MSFTNGP11.phx.gbl...
"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

Nov 16 '05 #4

P: n/a
"Bilo" <ju********@yahoo.com> wrote in
news:#p**************@TK2MSFTNGP12.phx.gbl:
I dont know what i am doing false.
the code :

private void button2_Click(object sender, System.EventArgs e)
{
foreach (string filename in listBox1.SelectedItems)
listBox1.Items.Remove(filename);
}
gives me the error :

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.
at
System.Windows.Forms.EntryEnumerator.System.Collec tions.IEnumerator.Mov
eNext ()

Anyone can help?


to keep from duplicating all those strings, how bout this?
while (listBox1.SelectedItems.Count > 0)
{
listBox1.Items.Remove(listBox1.SelectedItems[0]);
}
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.