Re: listBox.Items.Remove help

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Matt Berther (mberther_at_hotmail.com)
Date: 04/23/04


Date: Thu, 22 Apr 2004 17:42:00 -0700

Hello 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.Mo
> veNext
> ()

You can't modify a collection if you are iterating it with an Enumerator.

It appears to me that this line may suffice for what you're trying to accomplish.

listBox1.Items.Clear();

--
Matt Berther
http://www.mattberther.com

Quantcast