1.添加项
----------------方法一--------------
checkedListBox1.Items.Add("蓝色");
checkedListBox1.Items.Add("红色");
checkedListBox1.Items.Add("黄色");
----------------方法二--------------
checkedListBox1.DataSource = dtblTable;
checkedListBox1.ValueMember = "RoomName";
checkedListBox1.DisplayMember = "RoomName";
2. 判断第i项是否选中,选中为true,否则为false
if(checkedListBox1.GetItemChecked(i))
{
return true;
}
else
{
return false;
}
3. 设置第i项是否选中
checkedListBox1.SetItemChecked(i, true); //true改为false为没有选中。
4.设置全选
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
5.得到全部选中的值 ,并将选中的项的文本组合成为一个字符串。
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. 设置CheckedListBox中第i项的Checked状态
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
7.获取绑定的Value值
checkedListBox1.SelectedValue.ToString();
8. 清除checkedListBox1中所有的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
}
9. //反向选择checkedListBox1的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.SetItemChecked(i, false);
}
else
{
checkedListBox1.SetItemChecked(i, true);
}
}