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

Était-ce utile?

La solution

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

Autres conseils

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);
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top