Wednesday, March 28, 2012

System.InvalidOperationException: Collection was modified; enumeration operation may not e

hi all,

i am getting the following error when i am trying to move the ltems form listbox1 to listbox2

"System.InvalidOperationException: Collection was modified; enumeration operation may not execute"

ForEach itemAs ListItemIn Listbox1.Items

If item.SelectedAndNot Listbox2.Items.Contains(item)Then

Dim newItemAsNew ListItem(item.Text, item.Value)

Listbox2.Items.Add(newItem)

listbox1.Remove(newitem)

EndIf

Next

thanks.

You can't modify the listbox1 item collection while you are iterating over it using "for each". Wait until you add everything to listbox2, and then call Clear() on listbox1.

0 comments:

Post a Comment