I am encountering an odd issue whereby I can move items from one list box to another, but cannot move any items back to the original list box. Here is my code:
private void MoveListBoxItems(ListBox from, ListBox to)
{
for(int i = 0; i < first_listbox.Items.Count; i++)
{
if (first_listbox.Items[i].Selected)
{
to.Items.Add(from.SelectedItem);
from.Items.Remove(from.SelectedItem);
}
}
from.SelectedIndex = -1;
to.SelectedIndex = -1;
}
protected void Button2_Click(object sender, EventArgs e)
{
MoveListBoxItems(first_listbox, second_listbox);
}
protected void Button1_Click(object sender, EventArgs e)
{
MoveListBoxItems(second_listbox, first_listbox);
}
The button2 event works fine, however the button1 event does not. The list boxes are not data bound and I have manually added items to them.
Maybe there is something very obvious that I am missing here?
Thanks for your help in advance.
Change it to this:
Your original method was using
first_listboxin these two places, instead offrom. Also, I imagine your code does not work if more than one item is selected.