Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 209873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:57:17+00:00 2026-05-11T17:57:17+00:00

I have two ListBoxes, lstAvailableColors and lstSelectedColors. Between each listbox are two buttons, Add

  • 0

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);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:57:17+00:00Added an answer on May 11, 2026 at 5:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two listboxes and two buttons. Each listbox is in its own div
If I have two listboxes, with a button between them, how do I update
I have two list boxes, a datagridview and an Add and Remove button. Listbox1
I am trying to create a control which contains two listboxes with add/remove buttons
i have two ListBoxes (in a Silverlight 3 Application), each wrapped with a ListBoxDragDropTarget.
I have created an item swapper control consisting in two listboxes and some buttons
I have a vertical StackPanel with two elements: a Button and a ListBox. How
I have a WPF TabControl with two TabItems. Each TabItem contains a ListBox with
I am trying to implement drag and drop between two listboxes. I have a
I have two listboxes which I both bind to the same selected index. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.