0
votes

listbox

I have to move an item from one listbox to another listbox, as the above image indicates.

  • > button move one or multiple selected item from listbox1 to listbox2
  • >> button move all items from listbox1 to listbox2
  • << button delete all items from listbox2
  • < button delete selected item from listbox2
  • also I have a reset button to clear listbox2

Now the problem occurs when I click the <<, <, and reset button; that is, listbox2 items are not deleted.

protected void btn1_Click(object sender, EventArgs e)
    {
        lbltext.Visible = false;
        if (lstBoxAreaOfResponsibility.SelectedIndex >= 0)
        {
            foreach (ListItem Item in lstBoxAreaOfResponsibility.Items)
            {
                if (Item.Selected == true)
                {
                    lstBoxAreaOfResponsibility2.Items.Add(Item);
                }
            }
        }
        else
        {
            lbltext.Visible = true;
            lbltext.Text = "Please select atleast one in Area Of Responsibility to move";
        }
    }

    protected void btn2_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        if (lstBoxAreaOfResponsibility.Items.Count != 0)
        {
            foreach (ListItem Item in lstBoxAreaOfResponsibility.Items)
            {
                lstBoxAreaOfResponsibility2.Items.Add(Item);
            }
        }
        else
        {
            lbltext.Visible = true;
            lbltext.Text = "There Is no Item In ListBox 1 to Move";
        }
    }

    protected void btn3_Click(object sender, EventArgs e)
    {
        lbltext.Visible = false;
        if (lstBoxAreaOfResponsibility2.Items.Count != 0)
        {
            foreach (ListItem Item in lstBoxAreaOfResponsibility2.Items)
            {
                lstBoxAreaOfResponsibility2.Items.Remove(Item);
            }
        }
        else
        {
            lbltext.Visible = true;
            lbltext.Text = "There Is No Item To Move";
        }
    }

    protected void btn4_Click(object sender, EventArgs e)
    {
        lbltext.Visible = false;
        if (lstBoxAreaOfResponsibility2.SelectedIndex >= 0)
        {
            foreach (ListItem Item in lstBoxAreaOfResponsibility2.Items)
            {
                if (Item.Selected == true)
                {
                    lstBoxAreaOfResponsibility2.Items.Remove(Item);
                }
            }
        }
        else
        {
            lbltext.Visible = true;
            lbltext.Text = "Please select atleast one in Listbox2 to move";
        }
    }

after clicking the 3rd and 4th button, it is showing error like

"Collection was modified; enumeration operation may not execute."

1
For what it's worth, having a << (move all from box2 to box1) button and a "reset" button to clear box2 is redundant.TylerH

1 Answers

0
votes

You are modifying a collection while iterating over it. I don't have an IDE but can you try:

protected void btn3_Click(object sender, EventArgs e)
{
    lbltext.Visible = false;
    if (lstBoxAreaOfResponsibility2.Items.Count != 0)
    {
        lstBoxAreaOfResponsibility2.Items.Clear();
    }
    else
    {
        lbltext.Visible = true;
        lbltext.Text = "There Is No Item To Move";
    }
}

protected void btn4_Click(object sender, EventArgs e)
{
    lbltext.Visible = false;
    if (lstBoxAreaOfResponsibility2.SelectedIndex >= 0)
    {
        ListItem itemToDelete = new ListItem();
        foreach (ListItem Item in lstBoxAreaOfResponsibility2.Items)
        {
            if (Item.Selected == true)
            {
                itemToDelete = Item;
            }
        }
        lstBoxAreaOfResponsibility2.Items.Remove(itemToDelete);
    }
    else
    {
        lbltext.Visible = true;
        lbltext.Text = "Please select atleast one in Listbox2 to move";
    }
}