Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go through a checked listbox and check all items C#

I need to loop through a checked listbox, and for each of the items in it, I need to check them (basically like a "select all" function).

Is there a basic example you could give me to help me out please?

like image 895
user1290653 Avatar asked Apr 03 '12 19:04

user1290653


2 Answers

Use SetSelected and interate through all the Items

// Loop through and set all to selected.
for (int x = 0; x < listBox1.Items.Count; x++)
{
   listBox1.SetSelected(x, true);
}

To check the items, use SetItemChecked

// Loop through and set all to checked.
for (int x = 0; x < listBox1.Items.Count; x++)
{
   listBox1.SetItemChecked(x, true);
}
like image 75
SwDevMan81 Avatar answered Sep 26 '22 09:09

SwDevMan81


You can look through all the items as ListItems:

foreach (ListItem li in CheckBoxList1.Items)
{
    li.Selected = true;
}
like image 35
Khan Avatar answered Sep 23 '22 09:09

Khan