checkedListBox应用示例
下面是一个向checkedListBox中动态添加chenckbox控件的例:
一、首先在项目窗体上插入checkedListBox控件,由于默认的是单列显示,为了使其能显示多例要将其属性MultiColumn设置为true;
二、动态添加chenckbox:
for (int i = 0; i < 8; i++)
{
checkedListBox1.Items.Add(i.ToString());
}
{
checkedListBox1.Items.Add(i.ToString());
}
三、设置其是否选中的的状态:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (i%2==0)
{
checkedListBox1.SetItemChecked(i, true);
break;
}
}
{
if (i%2==0)
{
checkedListBox1.SetItemChecked(i, true);
break;
}
}
四、获取被选中的项:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
for (int j = 0; j < checkedListBox1.Items.Count; j++)
{
if (checkedListBox1.GetItemChecked(j))
{
MessageBox(j.ToString());
}
}
{
if (checkedListBox1.GetItemChecked(j))
{
MessageBox(j.ToString());
}
}