while(listbox.SelectedItems.Count > 0)
{
listbox.Items.Remove(listbox.SelectedItem);
}
Removing more than 1 item in listbox
-
01-10-2022 - |
Вопрос
please help me how can remove more than 1 item in a listbox
.I know the code for removing 1 item: listbox.Items.RemoveAt(i)
but for more than 1 item selected in a listbox,what is the code? write the code for example in button-click event.I have only a button and a listbox in my winform.(write codes in C#)
Решение
Другие советы
ListBox1.ClearSelection();
//or
foreach (ListItem listItem in ListBox1.Items)
{
listItem.Selected = false;
}
List<ListItem> itemsToRemove = new List<ListItem>();
foreach (ListItem listItem in ListBox1.Items)
{
if (listItem.Selected)
itemsToRemove.Add(listItem);
}
foreach (ListItem listItem in itemsToRemove)
{
ListBox1.Items.Remove(listItem);
}
Не связан с StackOverflow