Use an integer counter, start from the end of the Collection, and work your
way back to the beginning. Something like (not checked for typos):
for (int intCt = mymListBox.Items.Count - 1; intCt > -1; intCt--)
{
if ((Convert.ToString(mymListBox.Items[intCt].Value)).StartsWith(string))
chosenPractices.Items.Remove(mymListBox.Items[intCt]);
}
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.
"Will" <> wrote in message
news:061401c368f1$e58c9ab0$...
> I'm trying to run through all of the listitems in a
> listbox with a foreach loop. it looks like this:
>
>
> foreach (ListItem temp in mymListBox.Items) {
> if ((temp.ToString()).StartsWith(string)) {
> chosenPractices.Items.Remove(temp);
> }
> }
>
> I get the following error on the very first line:
>
> System.InvalidOperationException: Collection was modified;
> enumeration operation may not execute.
>
> please help!
>
>
> Will