Collection was modified; enumeration may not execute error when removing a ListItem from a LIstBox
I have two ListBoxes, lstAvailableColors and lstSelectedColors. Between each listbox are two buttons, Add and Remove. When a color or colors is selected in lstAvailableColors and the Add button is clicked, I want to remove them from lstAvailableColors and display them in lstSelectedColors. Also, if colors are selected in lstSelectedColors and the Remove button is clicked, I want to remove the colors from lstSelectedColors and add them back to lstAvailableColors. When I do this, I get the following error when it removes the item:
Collection was modified; enumeration operation may not execute.
Here is the code for the Add Button and the Remove Button:
Add:
protected void btnAdd_Click(object sender, EventArgs e)
{
foreach (ListItem item in lstAvailableColors.Items)
{
if (item.Selected)
{
lstSelectedColors.Items.Add(item);
lstAvailableColors.Items.Remove(item);
}
}
}
Remove:
protected void btnRemove_Click(object sender, EventArgs e)
{
foreach (ListItem item in lstSelectedColors.Items)
{
if (item.Selected)
{
lstAvailableColors.Items.Add(item);
lstSelectedColors.Items.Remove(item);
}
}
}
Solution 1:
It's not possible to modify a collection while you're enumerating it in .Net. You need to separate out your enumeration and remove code into different blocks. Here is a quick sample on how to do that in without LINQ
protected void btnAdd_Click(object sender, EventArgs e)
{
var selected = new List<ListItem>();
foreach (ListItem item in lstAvailableColors.Items)
{
if (item.Selected)
{
selected.Add(item);
lstSelectedColors.Items.Add(item);
}
}
foreach (ListItem item in selected)
{
lstAvailableColors.Items.Remove(item);
}
}
And here's a more concise version using LINQ
var selected = lstAvailableColors.Cast<ListItem>().Where(i => i.Selected).ToList();
selected.ForEach( x => { lstSelectedColors.Items.Add(x); });
selected.ForEach( x => { lstAvailableColors.Items.Remove(x);});
EDIT
The LINQ version works in two parts. The first part is the first line which finds the currently selected items and stores the value in a List<ListItem>
. It's very important that the line contain the .ToList() call because that forces the query to execute immediately vs. being delayed executed.
The next two lines iterate through each value which is selected and remove or add it to the appropriate list. Because the selected list is already stored we are no longer enumerating the collection when we modify it.
Solution 2:
You cannot modify an collection while you are using an Enumerator for this collection, what the for each statement does.
You have to loop over the data with a normal for loop and then you can modify the collection, but you must be careful to correctly update the current index if you insert or remove elements. If you just add or remove elements and don't insert some, iterating from the last element to the first will do.
protected void btnAdd_Click(object sender, EventArgs e)
{
for (Int32 i = lstAvailableColors.Items.Count; i >= 0; i--)
{
ListItem item = lstAvailableColors.Items[i];
if (item.Selected)
{
lstSelectedColors.Items.Add(item);
lstAvailableColors.Items.Remove(item);
}
}
}