Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically selecting items in winforms checkboxlist

I can't figure out how to programmatically select items in checkboxlist.

This method of cource doesn't compile, but I want to show you what a result I want to get.

public ColumnsSelector(Dictionary<string, bool> dataPropertyNames)
            : this()
        {
            foreach (var item in dataPropertyNames)
            {
                checkedListBox1.Items.Add(item.Key);
                checkedListBox1.Items[checkedListBox1.Items.IndexOf(item.Key)].Checked = item.Value;
            }
        }

How do you force with this problem?

like image 656
user278618 Avatar asked Dec 28 '22 05:12

user278618


1 Answers

Use CheckedListBox.SetItemCheckState:

checkedListBox.SetItemCheckState(checkedListBox1.Items.Count - 1, CheckedState.Checked);

which works for checked, unchecked, and indeterminate. You can also use CheckedListBox.SetItemChecked:

checkedListBox.SetItemChecked(checkedListBox1.Items.Count - 1, true);
like image 73
David Norman Avatar answered May 11 '23 14:05

David Norman