c# - Can move multiple items simultaneously in ListBox up the list, but not down? -
i have strange bug i'm having hard time figuring out.
if select multiple items in listbox , select arrow, items move 1 space if 1 unit. however, not happen when clicking down arrow move items down 1 spot each in list.
instead, if select number of items , click down arrow, none of items moves @ all, , if select odd number of items , click down arrow, bottom item moves down 1 space, while others stay are.
anyone have idea why might happening?
here code i'm using handle moving of items:
private void moveiteminselecteditemslistbox(object sender) { if (selecteditemslistbox.selecteditems.count > 0) { // build list of items moved list<object> listofitemstobemoved = new list<object>(); (int = 0; < selecteditemslistbox.selecteditems.count; i++) { listofitemstobemoved.add(selecteditemslistbox.selecteditems[i]); } (int k = 0; k < listofitemstobemoved.count; k++) { var selecteditem = listofitemstobemoved[k]; int selecteditemindex = selecteditemslistbox.items.indexof(selecteditem); int numberofitemsinlist = selecteditemslistbox.items.count; // if arrow clicked if (sender.tostring().contains("▲")) { if (selecteditemindex == 0) { selecteditemslistbox.items.remove(selecteditem); selecteditemslistbox.items.insert(numberofitemsinlist - 1, selecteditem); selecteditemslistbox.setselected(numberofitemsinlist - 1, true); } else { selecteditemslistbox.items.remove(selecteditem); selecteditemslistbox.items.insert(selecteditemindex - 1, selecteditem); selecteditemslistbox.setselected(selecteditemindex - 1, true); } } // if down arrow clicked else if (sender.tostring().contains("▼")) { if (selecteditemindex == numberofitemsinlist - 1) { selecteditemslistbox.items.remove(selecteditem); selecteditemslistbox.items.insert(0, selecteditem); selecteditemslistbox.setselected(0, true); } else { selecteditemslistbox.items.remove(selecteditem); selecteditemslistbox.items.insert(selecteditemindex + 1, selecteditem); selecteditemslistbox.setselected(selecteditemindex + 1, true); } } } } }
it's problem direction of for
loop: in essence, when moving items down, if moving up through list while checking items move down, end overlaps , other weird things. solution loop going down items if you're moving items down:
if (selecteditemslistbox.selecteditems.count > 0) { list<object> itemstomove = (from object item in selecteditemslistbox.selecteditems select item).tolist(); int numitems = selecteditemslistbox.items.count; // if arrow clicked if (sender.tostring().contains("▲")) { (int = 0; < itemstomove.count; i++) { var selecteditem = itemstomove[i]; int oldindex = selecteditemslistbox.items.indexof(selecteditem); int newindex = oldindex == 0 ? numitems - 1 : oldindex - 1; selecteditemslistbox.items.remove(selecteditem); selecteditemslistbox.items.insert(newindex, selecteditem); selecteditemslistbox.setselected(newindex, true); } } // if down arrow clicked else if (sender.tostring().contains("▼")) { (int = itemstomove.count - 1; >= 0; i--) { var selecteditem = itemstomove[i]; int oldindex = selecteditemslistbox.items.indexof(selecteditem); int newindex = oldindex == numitems - 1 ? 0 : oldindex + 1; selecteditemslistbox.items.remove(selecteditem); selecteditemslistbox.items.insert(newindex, selecteditem); selecteditemslistbox.setselected(newindex, true); } } }
(i took liberty of making of code smaller, e.g. changing variable names.)
Comments
Post a Comment