C# WinForm CheckedListBox 使用的相关简单总结

  • Post author:
  • Post category:其他


1. 如何:确定 Windows 窗体 CheckedListBox 控件中的选定项

当显示 Windows 窗体 CheckedListBox 控件中的数据时,

可以循环访问 CheckedItems 属性中存储的集合,或者使用 GetItemChecked 方法逐句通过列表来确定选中的项。

GetItemChecked 方法接受一个项索引号作为参数,并返回 true 或 false。

可能与您期望的相反,SelectedItems 和 SelectedIndices 属性并不确定哪些项已选中;

它们确定哪些项为突出显示。


// Determine if there are any items checked.

if(checkedListBox1.CheckedItems.Count != 0)

{


// If so, loop through all checked items and print results.

string s = “”;

for(int x = 0; x <= checkedListBox1.CheckedItems.Count – 1 ; x++)

{


s = s + “Checked Item ” + (x+1).ToString() + ” = ” + checkedListBox1.CheckedItems[x].ToString() + “\n”;

}

MessageBox.Show (s);

}

即CheckedItems    表示 CheckBox选中 的项

SelectedItems   表示 当前反蓝显示的项  SelectedIndex

2.常用属性:

CheckOnClick

ThreeDCheckBoxes

3.常用事件:

ItemCheck

SelectedIndexChanged

4.不用再试图 生成的CheckedListBox有部分选项不可用 部分选项可用

如果想实现上面的效果 可以利用Panel等容器结合CheckBox等控件来实现

因为this.checkedListBox1.Items[i]是Object 不支持Enabled