listBox1.Items.Remove help

  • Thread starter Thread starter Bilo
  • Start date Start date
B

Bilo

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.Collections.IEnumerator.MoveNext
()

Anyone can help?
 
Bilo said:
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.Collections.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);
 
...
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
 
Ahh first store the items.
Thx you.

Bjorn Abelli said:
...
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
 
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.Collections.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]);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top