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

后端 未结 9 673
抹茶落季
抹茶落季 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:56

    The problem you face is that you can't modify the collection you itterate thru. You could solve this by using a single linq:

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        lstAvailableColors.Items.RemoveAll(ac => ac.Selected);
    }
    

提交回复
热议问题