Solved

move items from one listbox to another

Posted on 2016-09-21
2
26 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
2 Comments
 
LVL 32

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now