C # Checkedlistbox if checkbox

Is it possible to apply .Checked == to a checklistbox like in a checkbox?

If you do it like with a checkbox, it doesn't work

if(checkedListBox1.Items[2].Checked==true) { } 
+6
source share
4 answers

You need the GetItemCheckState method.

Use as follows:

 if(checkedListBox1.GetItemCheckState(2) == CheckState.Checked) { } 
+9
source

You can use it that way

 if (checkedListBox1.CheckedItems.Contains("ItemWithIndex2")) { MessageBox.Show("Test"); } 
+6
source

Try something like ...

 checkedListBox1.GetItemChecked(i) foreach(int indexChecked in checkedListBox1.CheckedIndices) { // The indexChecked variable contains the index of the item. MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + checkedListBox1.GetItemCheckState(indexChecked).ToString() + "."); } 
+2
source

I'm not sure I understand your question, do you want to check if at least 1 element in the list is checked? If so, you can do it

 if(checkedListBox1.Items.Any(item=>item.Checked)) { } 
-2
source

Source: https://habr.com/ru/post/970425/


All Articles