ListBox selection removal
-
yeah, I have a listBox that the user can select multiple items in the list, how do I go about removing the selected items? I tried so many things, but I haven't had any success, I'm thankful for any help. Stephen
Remove the selected items from the list?
for(int i = lstBox.SelectedItems.Count; i >= 0; i--) lstBox.Items.RemoveAt(i);
You have to do it bottom-up. If you do top-down, when you remove an item on the top, the index of the items below it change. You could also do this:
while(lstBox.SelectedItems.Count > 0) lstBox.Items.Remove(lstBox.SelectedItems[0]);
but I think the first way is easier to understand. I hope this helps! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005
-
Remove the selected items from the list?
for(int i = lstBox.SelectedItems.Count; i >= 0; i--) lstBox.Items.RemoveAt(i);
You have to do it bottom-up. If you do top-down, when you remove an item on the top, the index of the items below it change. You could also do this:
while(lstBox.SelectedItems.Count > 0) lstBox.Items.Remove(lstBox.SelectedItems[0]);
but I think the first way is easier to understand. I hope this helps! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005