Collection was modified; enumeration may not execute error when removing a ListItem from a LIstBox

后端 未结 9 647
抹茶落季
抹茶落季 2020-11-29 12:30

I have two ListBoxes, lstAvailableColors and lstSelectedColors. Between each listbox are two buttons, Add and Remove. When a color or colors is selected in lstAvailableCol

9条回答
  •  醉梦人生
    2020-11-29 12:50

    You cannot modify a collection you are iterating on. In general, a good solution for this type of problem is to create an empty collection, and in your iterator, copy over all of the elements you do NOT want to remove; after the iteration is complete, replace the original collection with your new collection.

提交回复
热议问题