Summary of Usage of C CheckedListBox Control

  • 2021-11-14 06:53:04
  • OfStack

1 generally: foreach (object obj in checkedListBox1.SelectedItems) can iterate over the selected value.

In fact, only highlighted values are traversed here, not ticked values. The following code is used to traverse the ticked values:


for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
  if (checkedListBox1.GetItemChecked(i))
  {
    MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]));
  }
}

Recently used checklistbox control, in the use of its process, spent a lot of time, here I collected its relevant code segment, I hope to help you.

1. Add items


checkedListBox1.Items.Add( "Blue" ); 
checkedListBox1.Items.Add( "Red" ); 
checkedListBox1.Items.Add( "Yellow" );

2. Judge whether the item i is selected, and the selected item is true, otherwise it is false


if ( checkedListBox1.GetItemChecked(i) ) 
{
   return true;
} 
else
{
   return false; 
}

3. Set whether item i is selected


checkedListBox1.SetItemChecked(i, true); //true Replace with false Is not selected. 

4. Set Select All

Add an checkbox control named select_all, which controls whether checkedListBox is selected all or not.


private void select_all_CheckedChanged(object sender, EventArgs e) 
{ 
   if(select_all.Checked) 
{
     for (int j = 0; j < checkedListBox1.Items.Count; j++) 
        checkedListBox1.SetItemChecked(j, true); 
}
else 
{
for (int j =0; j < checkedListBox1.Items.Count; j++) 
   checkedListBox1.SetItemChecked(j, false);
}
}

5. Get all the selected values and combine the text of the selected items into a string.


string strCollected = string.Empty;
 for (int i = 0; i < checkedListBox1.Items.Count; i++)
 {
   if (checkedListBox1.GetItemChecked(i))
   {
     if (strCollected == string.Empty)
     {
        strCollected = checkedListBox1.GetItemText(
checkedListBox1.Items[i]);
     }
     else
     {
        strCollected = strCollected +  " / "  + checkedListBox1.
GetItemText(checkedListBox1.Items[i]);
      }
    }
}

6. Set the Checked status of item i in CheckedListBox


checkedListBox1.SetItemCheckState(i, CheckState.Checked);

7.


private void checkBoxAll_CheckedChanged(object sender, EventArgs e) 
{ 
   if (checkBoxAll.Checked) 
   { 
     // Is selected, the CheckedListBox All entries in the Checked Status  
     for (int i = 0; i < checkedListBoxLayerControl.Items.Count;
          i++) 
     {   
checkedListBoxLayerControl.SetItemCheckState(i, 
    CheckState.Checked); 
} 
}
else 
{ 
   // Otherwise it becomes Unchecked Status  
  for (int i = 0;
 i < checkedListBoxLayerControl.Items.Count; i++) 
{
    checkedListBoxLayerControl.SetItemCheckState(i, CheckState.Unchecked); 
}       
}
}

8. checkedListBox radio settings (code implementation)


private void chkl_ItemAuditing_ItemCheck(object sender,  
ItemCheckEventArgs e)
{ 
   if (chkl_ItemAuditing.CheckedItems.Count > 0) 
  { 
     for (int i = 0; i < chkl_ItemAuditing.Items.Count; i++) 
     {
     if (i != e.Index) 
     { 
       this.chkl_ItemAuditing.SetItemCheckState(i, 
       System.Windows.Forms.CheckState.Unchecked); 
     } 
  } 
} 
}

9. checkedListBox1 displays all records corresponding to keywords in 1 database


for (int i = 0; i < table.Rows.Count; i++) 
{ 
  string name = table.Rows["myname"].ToString(); 
  string paw = table.Rows["mypaw"].ToString(); 
  checkedListBox1.Items.Add(name + paw); 
}
 

10.


checkedListBox1.Items.Add( "Blue" ); 
checkedListBox1.Items.Add( "Red" ); 
checkedListBox1.Items.Add( "Yellow" );
0

11. Clear all options in checkedListBox1


checkedListBox1.Items.Add( "Blue" ); 
checkedListBox1.Items.Add( "Red" ); 
checkedListBox1.Items.Add( "Yellow" );
1

12.


checkedListBox1.Items.Add( "Blue" ); 
checkedListBox1.Items.Add( "Red" ); 
checkedListBox1.Items.Add( "Yellow" );
2

13.


for (int i = 0; i < checkedListBox1.Items.Count; i++) 
{
     if (checkedListBox1.GetSelected(i)) 
     {
          MessageBox.Show(checkedListBox1.CheckedItems.ToString());
     }
}

14.


checkedListBox1.Items.Add( "Blue" ); 
checkedListBox1.Items.Add( "Red" ); 
checkedListBox1.Items.Add( "Yellow" );
4

15.


checkedListBox1.Items.Add( "Blue" ); 
checkedListBox1.Items.Add( "Red" ); 
checkedListBox1.Items.Add( "Yellow" );
5

16.


checkedListBox1.Items.Add( "Blue" ); 
checkedListBox1.Items.Add( "Red" ); 
checkedListBox1.Items.Add( "Yellow" );
6

17.


checkedListBox1.Items.Add( "Blue" ); 
checkedListBox1.Items.Add( "Red" ); 
checkedListBox1.Items.Add( "Yellow" );
7

Related articles: