listbox - Moving item from one list box to another list box (c# webforms) -
i encountering odd issue whereby can move items 1 list box another, cannot move items original list box. here code:
private void movelistboxitems(listbox from, listbox to) { for(int = 0; < 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, button1 event not. list boxes not data bound , have manually added items them.
maybe there obvious missing here?
thanks in advance.
change this:
private void movelistboxitems(listbox from, listbox to) { for(int = 0; < from.items.count; i++) { if (from.items[i].selected) { to.items.add(from.selecteditem); from.items.remove(from.selecteditem); // should this: to.items.add(from.items[i]); from.items.remove(from.items[i]); } } from.selectedindex = -1; to.selectedindex = -1; }
your original method using first_listbox
in these 2 places, instead of from
. also, imagine code not work if more 1 item selected.
Comments
Post a Comment