Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through CheckedListBox in WinForms?

If I have Checked list box in Win forms which I fill like this

List<Tasks> tasks = db.GetAllTasks();
        foreach (var t in tasks)
            tasksCheckedListBox.Items.Add(t.Name);

How can I iterate tasksCheckedListBox.Items and set some check boxes as checked?

Thanks

like image 995
eomeroff Avatar asked Jan 22 '23 15:01

eomeroff


1 Answers

The add method takes an optional IsChecked parameter. You can then add your objects into the checked list box in the correct state.

List<Tasks> tasks = db.GetAllTasks();
        foreach (var t in tasks)
            tasksCheckedListBox.Items.Add(t.Name, isChecked);

Or you can change the checked state of an item after you add it with something like this:

foreach(var task in tasks)
{
    tasksCheckedListBox.SetItemChecked(clb.Items.IndexOf(task), isChecked);
}
like image 88
Jake Pearson Avatar answered Feb 01 '23 15:02

Jake Pearson