Solved

move items from one listbox to another

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

18 Experts available now in Live!

Get 1:1 Help Now