How to set software state for all items in winforms Checkedlistbox software?

I am working on a Windows Form application. I want to check/uncheck all the checkboxes in the checklistbox.

I use the following code to generate checkboxes dynamically.

  var CheckCollection = new List<CheckedBoxFiller>(); foreach (DataRow dr in dt.Rows) CheckCollection.Add(new CheckedBoxFiller { Text = dr["ImageName"].ToString(), Value = dr["ImageId"].ToString() }); chklbEvidenceTags.DataSource = CheckCollection; chklbEvidenceTags.DisplayMember = "Text"; chklbEvidenceTags.ValueMember = "Value"; 

And this is the CheckboxFiller class

 private class CheckedBoxFiller { public string Text { get; set; } public string Value { get; set; } } 

Now I want to check/uncheck all checkboxes . How can I achieve this?

Any help would be helpful.

+7
source share
4 answers

I have found a solution.

  for (int i = 0; i < chklistbox.Items.Count; i++) chklistbox.SetItemCheckState(i, (state ? CheckState.Checked : CheckState.Unchecked)); 

state - boolen value.

+14
source

If you have a large list of items, this approach may be a little more effective for unchecking. This requires that you only follow the items that are actually checked:

  private void UncheckAllItems() { while (chklistbox.CheckedIndices.Count > 0) chklistbox.SetItemChecked(chklistbox.CheckedIndices[0], false); } 

And if you use several CheckedListBox controls throughout your project and want to take another step, you can add it as an extension method:

  public static class AppExtensions { public static void UncheckAllItems(this System.Windows.Forms.CheckedListBox clb) { while (clb.CheckedIndices.Count > 0) clb.SetItemChecked(clb.CheckedIndices[0], false); } } 

Call extension method:

  chklistbox.UncheckAllItems(); 
+8
source

Check / uncheck all elements of the list written under the code:

 if (checkBox1.Checked) { for (int i = 0; i < chkboxlst.Items.Count; i++) { chkboxlst.SetItemChecked(i, true); } } else { for (int i = 0; i < chkboxlst.Items.Count; i++) { chkboxlst.SetItemChecked(i, false); } } 
0
source

Remove or check all listItems Do the following code:

  boolean state =false;//False->Uncheck,true->Check for (int i = 0; i < chklistbox.Items.Count; i++) chklistbox.SetItemCheckState(i, (state ? CheckState.Checked : CheckState.Unchecked)); 
-3
source

All Articles