Question

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#)

Was it helpful?

Solution

while(listbox.SelectedItems.Count > 0)
 {
    listbox.Items.Remove(listbox.SelectedItem);
 }

OTHER TIPS

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);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top