一、绑定数据
1.直接绑定,如图所示。在属性中找到Items属性,添加集合,每行为一个结果。
2.代码绑定
checkedListBox1.Items.Add(string);
二、获取数据
1.所有值
checkedListBox1.Items
2.获取选中值
private List<string> GetCheckedItems(CheckedListBox clb)
{
List<string> result = new List<string>();
IEnumerator myEnumerator = clb.CheckedIndices.GetEnumerator();//获取选中项
int index;
while (myEnumerator.MoveNext())
{
index = (int)myEnumerator.Current;
clb.SelectedItem = clb.Items[index];//取到选中项的值
result.Add(clb.Text());
}
return result;
}
方法二、
List<string> result =new List<String>();
for(int i =0;i<CheckedListBox.items.count;i++)
{
if(CheckedListBox.GetItemChecked(i))
{
result.Add(CheckedListBox.items[i]);
}
}
三、选中值
CheckedListBox.SetItemChecked(i,true);//i为序号,true为选中,反之则取消选中
如有其他思路或者答案,欢迎讨论
我是小赖,一个学习C#的混子。