Solved

move items from one listbox to another

Posted on 2016-09-21
2
37 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 33

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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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