1. Common properties, methods and events
1. Common attributes
( 1) CheckOnClick attribute
The CheckOnClick attribute is a Boolean value. If it is True, the item can be checked by clicking the item; if it is false, the item must be double-clicked to be checked.
(2) ColumnWidth property
The ColumnWitdh attribute is integer data, indicating the column width of each column of a multi-column entry. This attribute is only meaningful when the MultiColumn attribute (allowing multiple columns to display) is set to true.
Multiple columns are displayed as shown below:
(3) MultiColumn property
The MultiColumn attribute is a Boolean value indicating whether to enable multi-column display items. This property is used together with the ColumnWidth property.
(4) SelectMode attribute
This attribute indicates whether the list will be single-selected or multi-selected. "Selected" here means selected instead of "checked". After selection, the item will be highlighted, but the small box on the left of the item will not be checked. In the CheckedLIstBox control, multiple selection is not supported, and only two values of this property are valid, namely None and One. None means that the item cannot be selected, and the small box on the left cannot be checked; One means that only one item can be selected (but multiple items can be selected by checking).
(5) Sorted property
This property is a Boolean value, if true, the entries are sorted alphabetically, if false, they are not sorted.
(6) Count property
This property represents the total amount of entries in the list. Usage is as follows:
int conut=checkedListBox1.Items.Count
(7) Items property
Get the collection of items in the list, and get the specified item by subscript.
object item=checkedListBox1.Items[i];
(8) SelectedItem property and SelectedItems property
The SelectedItem property is used to obtain the selected items, and the SelectedItems property is an array, which saves the collection of selected items, and the items can be obtained through the subscript. The usage is as follows:
object item =checkedListBox1.SelectedItem;
object item=checkedListBox1.SelectedItems[i];
checkedListBox1.Items.Add(item);
2. Common methods
(1) Add method
This method is used to add entries dynamically. Usage is as follows:
checkedListBox1.Items.Add("entry one");//parameter can be a string or an item
or checkedListBox1.Items.Add("entry one", true);//The second parameter is a Boolean type, indicating whether to let the item be checked
(2) RemoveAt method
This method is used to remove the specified item, and the parameter is the index value of the item. Usage is as follows:
checkedListBox1.Items.RemoveAt(1)。
(3) Insert method
The function of this method is to insert an item at the specified position, with two parameters, index and item. index is the index of the item, and item can be an item or a character. Its usage is as follows:
checkedListBox1.Items.Insert(i,"Item 1");
(4) GetItemChecked method
This method returns whether the i-th item is checked (not selected), if it is, it returns true, otherwise it is false. The parameter is the index value of the entry. Its usage is as follows:
bool isChecked=checkedListBox1.GetItemChecked(i);
(5) SetItemChecked method
Set whether the i-th entry is selected, there are two parameters, the first is the index, and the second is a Boolean value. If the second parameter is true, the i-th item is set to be checked, otherwise it is set to be unchecked. Its usage is as follows:
checkedListBox1.SetItemChecked(i,true);
(6)Clear方法
该方法的作用是清除所有的条目。其用法如下:
checkedListBox1.Clear();
3.常用事件
(1)ItemCheck事件
当条目被勾选时发生,其用法如下:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
//通过索引引用勾选的条目
textBox1.Text = textBox1.Text +"\r\n"+ checkedListBox1.Items[e.Index].ToString();
}
(2)SelectedIndexChanged和SelectedValuedChanged事件
这两个事件都是在条目被选中时发生(不一定被勾选)
二、应用实例
1.实例描述
使用CheckedBox和CheckedListBox实现如下图所示的功能:点击勾选坐标的复选框,游边的CheckedLIstBox中就会添加一个条目,并且条目是多列显示。点击勾选右边的CheckedLIstBox中的条目,就会将条目的内容显示在下方的文本框中。
2.属性设置
在属性栏中,将checkedListBox1的MultiColumn属性设为true,以实现条目的多列显示;
3.事件处理
(1)checkedBox1的CheckedChanged事件
首先,自定义一个方法用来处理checkedBox1的CheckedChanged事件:
private void CheckedChanged(object sender,EventArgs e)
{
CheckBox cb = (CheckBox)sender;//sender是被勾选的CheckedBox,引用之前先强制转换为CheckedBox类型
if (cb.Checked)
{
checkedListBox1.Items.Add(cb.Text);
}
}
然后,在CheckedBox的属性栏中,将所有的CheckedBox的CheckedChanged事件处理方法都选为CheckedChanged,这样的话,多个复选框就可以共用一个事件处理方法了。如图:
(2)checkedListBox1的ItemCheck事件
4.实现代码