Solved

move items from one listbox to another

Posted on 2016-09-21
2
44 Views
Last Modified: 2016-09-21
Hi,

The following is my code, I am trying to move items from one listbox to another. But I am getting

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

I guess it is because as item in listBox1 was removed, the count is different.

    protected void MoveMultibtn_Click(object sender, EventArgs e)
    {

        if (listBox1.SelectedIndex == -1)
        {
            testBox.Text = "Please select an item";
        }
        else
        {
                foreach (ListItem item in listBox1.Items)
                {
                    if (item.Selected)
                    {
                        listBox2.Items.Add(item);
                        listBox1.Items.Remove(listBox1.SelectedItem);
                    }
                }


        }



    }

Open in new window

0
Comment
Question by:mcrmg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 41809431
When enumerating a list to remove items, you have to do it in reverse.  This means that you do one of two things:

A.  Use a reversed for...loop:
for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
	if ((listBox1.Items[i] as ListItem).Selected)
	{
		listBox2.Items.Add(listBox1.Items[i]);
		listBox1.Items.Remove(listBox1.Items[i]);
	}
}

Open in new window

B.  Use a reversed foreach...loop:
foreach (var item in listBox1.Items.Cast<ListItem>().Reverse())
{
	if (item.Selected)
	{
		listBox2.Items.Add(item);
		listBox1.Items.Remove(item);
	}
}

Open in new window


-saige-
0
 

Author Closing Comment

by:mcrmg
ID: 41809509
thank you very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question