checklistbox Controls
1. Add Item
checkedListBox1.Items.Add ( "blue");
checkedListBox1.Items.Add ( "red");
checkedListBox1.Items.Add("黄色");
2. determine whether the item i selected, select is true, false otherwise
if(checkedListBox1.GetItemChecked(i))
{
return true;
}
else
{
return false;
}
3. Set the item i is selected
checkedListBox1.SetItemChecked (i, true); // true to false is not checked.
4. Set Select
Add a checkbox control named select_all, and its control is checkedListBox Select All or Select None.
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);
}
}
The obtained values of all selected, and the selected text string into a combination of the items.
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 Checked state CheckedListBox i-th item
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
7.
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxAll.Checked)
{
// All entries will be selected in the CheckedListBox have become Checked state
for (int i = 0; i < checkedListBoxLayerControl.Items.Count;
i++)
{
checkedListBoxLayerControl.SetItemCheckState(i,
CheckState.Checked);
}
}
else
{
// otherwise become Unchecked status
for (int i = 0;
i < checkedListBoxLayerControl.Items.Count; i++)
{
checkedListBoxLayerControl.SetItemCheckState(i, CheckState.Unchecked);
}
}
}
8. checkedListBox radio set (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 display all records in a database corresponding to the keywords
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.
for(i=0;i<CheckedListBox.Items.Count;i++)
{
if(CheckedListBox.GetItemText(
CheckedListBox.Items) == "value you get")
{
CheckedListBox.SetItemChecked(i,true);
}
}
11. checkedListBox1 all options Clear
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
}
12. // Set the index to index entry is selected
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
13.
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetSelected(i))
{
MessageBox.Show(checkedListBox1.CheckedItems.ToString());
}
}
14.// all the options selected checkedListBox1
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
}
15.
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
// If checkedListBox1 of item i is selected,
// checkedListBox1 corresponding value is displayed
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.Items.ToString());
}
}
16. // Invert Selection option checkedListBox1
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.SetItemChecked(i, false);
}
else
{
checkedListBox1.SetItemChecked(i, true);
}
}
17. // checkedListBox1 selected items -> checkedListBox2
for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems);
// remove to remove a specific value, not the index, pay attention
this.checkedListBox1.Items.Remove(
this.checkedListBox1.CheckedItems);
}
Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/02/16/2354105.html