Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select all items in a listbox on checkbox checked?

I need to select all items in a ListBox when a CheckBox is clicked. Is it possible to select all items in the ListBox using a single line of code? Or will I have to loop through all items and set selected to true for each one of them?

like image 679
SO User Avatar asked Jun 01 '09 10:06

SO User


People also ask

How do I select multiple items in ListBox?

Choose Multiple Items from ListboxOn the worksheet, click on a cell that has a drop down list. The VBA listbox pops up automatically, and shows all the choices from the cell's drop down list. Add a check mark to one or more of the items in the list box. When you're finished selecting items, click the OK button.

How do I select from ListBox?

To select an item in a ListBox, we can use the SetSelect method that takes an item index and a true or false value where the true value represents the item to be selected. The following code snippet sets a ListBox to allow multiple selection and selects the second and third items in the list: listBox1.

How do you get the selected items and search the ListBox items?

To retrieve a collection containing all selected items in a multiple-selection ListBox, use the SelectedItems property. If you want to obtain the index position of the currently selected item in the ListBox, use the SelectedIndex property.

How do you check ListBox is selected or not?

To determine the items that are selected, you can use the Selected property of the list box. The Selected property of a list box is an array of values where each value is either True (if the item is selected) or False (if the item is not selected).


1 Answers

The fact is that ListBox.Items is a plain object collection and returns plain untyped objects, which cannot be multi-selected (by default).

If you want to multi-select all items, then this will work:

   for (int i = 0; i < myListBox.Items.Count;i++)    {        myListBox.SetSelected(i, true);    } 
like image 172
Mehdi LAMRANI Avatar answered Sep 21 '22 14:09

Mehdi LAMRANI