[Yugong Series] Detailed Explanation of the CheckedListBox Control on Winform Controls in September 2023


foreword

Winform controls are user interface elements in Windows Forms. They can be used to create various visual and interactive components of Windows applications, such as buttons, labels, text boxes, drop-down list boxes, check boxes, radio boxes, progress bars, etc. . Developers can use Winform controls to build user interfaces and respond to user actions to create powerful desktop applications.

1. Detailed explanation of CheckedListBox control

The CheckedListBox control is a commonly used control in Windows Forms, which is used to display a multi-selection list box that allows users to select multiple items.

Use the CheckedListBox control to present a set of related options to the user, from which the user can select any number of options. Suitable for scenarios that require the user to make multiple selections from a set of options.

Using the CheckedListBox control in Winform requires the following steps:

  1. Create a Windows Form application in Visual Studio, and then drag the CheckedListBox control from the Toolbox onto the form.
  2. Set the properties of CheckedListBox, including ItemHeight, CheckOnClick, SelectionMode, etc.
  3. To add list items, single or multiple items can be added using the Items property.
  4. Handle the Check event and respond accordingly according to the item selected by the user.

Here is a simple sample code:

private void Form1_Load(object sender, EventArgs e)
{
    
    
    // 添加列表项
    checkedListBox1.Items.Add("C#");
    checkedListBox1.Items.Add("Java");
    checkedListBox1.Items.Add("Python");
    checkedListBox1.Items.Add("C++");

    // 设置属性
    checkedListBox1.CheckOnClick = true;
    checkedListBox1.SelectionMode = SelectionMode.MultiSimple;
}

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    
    
    // 每次选择一个项时,都会触发该事件
    if (e.NewValue == CheckState.Checked)
    {
    
    
        MessageBox.Show(string.Format("您选择了:{0}", checkedListBox1.Items[e.Index].ToString()));
    }
}

In the above example, we first added some list items when the form loaded and set some properties of CheckedListBox. Then the ItemCheck event is processed, and the corresponding response is made according to the item selected by the user in the event handler.
insert image description here

1. Attribute introduction

1.1 CheckOnClick

The CheckedListBox control is a commonly used control in Windows Forms for selecting one or more items from a list. Among them, the CheckOnClick attribute is an attribute that controls whether the item is automatically selected when the user clicks the item in the list box.

When the CheckOnClick property is set to true, when an item is clicked, the selected state of the item is automatically toggled. For example, if you click an unchecked item, it will be checked; if you click a checked item, it will be unchecked. Also, if the mouse pointer hovers over an item for more than a brief period, the item will appear selected.

When the CheckOnClick property is set to false, the item is not automatically checked or unchecked when the item is clicked. Instead, clicking an item simply changes the focus of the list box so that the user can use the arrow keys on the keyboard to change the selected item.

It should be noted that when the CheckOnClick property is true, if you want to use the right mouse button to open the context menu, you need to set the control's ContextMenuStrip property to a valid context menu. Otherwise, selection automatically toggles its selected state when the user right-clicks the control.

1.2 ColumnWidth

The ColumnWidth property of the CheckedListBox control is used to set the column width of each item in the control. By default, each item in the CheckedListBox control is displayed at the default width, but if you need to display columns of different widths, you can use this property.

The value of this property is an integer value in pixels. If set to zero or a negative number, the default column width will be used. If you need to display multiple columns, you can set this property to a value greater than zero, and set the MultiColumn property of the CheckedListBox control to true.

For example, if you want to set the CheckedListBox control to display two columns, you can set the ColumnWidth property to half the width of the control, as follows:

checkedListBox1.ColumnWidth = checkedListBox1.Width / 2;
checkedListBox1.MultiColumn = true;

This will cause the CheckedListBox control to display two columns, each half the width of the control. If you need to display more columns, you can increase the value of the ColumnWidth property accordingly, and set the MultiColumn property to true.
insert image description here

2. Common scenarios

The CheckedListBox control is often used when the user selects multiple options from a list, where each option can be checked or unchecked. Specific usage scenarios include:

  1. Product feature selection: CheckedListBox can be used to allow users to select certain features or functions on the software or website. For example, a piece of video editing software may allow users to check certain options to enable specific editing functions.

  2. Option filtering: If you need to filter or search a large amount of data, you can use CheckedListBox to let the user choose which options to show or hide. For example, a product list on an e-commerce website, users can filter products by checking different options.

  3. Multiple choice: If you need to let the user choose between multiple options, you can use CheckedListBox to display these options and let the user choose more than one of them. For example, a tab control allows the user to select one or more tabs.

  4. File selection: In some cases, it is necessary to let the user select one or more files and add them to a specific collection. CheckedListBox can be used for this purpose. For example, a document editor could let the user select files to open and add them to the editor.

The CheckedListBox control is very suitable for situations where users need to choose from multiple options, and it is very helpful for some scenarios that require users to select multiple options.

3. Specific cases

A CheckBoxList control is a Winforms control that allows the user to select from multiple options. Each selection consists of a checkbox and corresponding text label. Users can click a checkbox to select or deselect an item. Following is a simple example showing how to use CheckBoxList control in Winforms application.

First, we need to open a new Winforms project in Visual Studio and add a CheckedListBox control to the form. You can drag it onto the form from the toolbox, or add it from the designer. The default name of the control is checkedListBox1.

Next, we need to add some code in the form's Load event to add some items to the CheckBoxList control. Here is a simple example:

private void Form1_Load(object sender, EventArgs e)
{
    
    
    checkedListBox1.Items.Add("Item 1");
    checkedListBox1.Items.Add("Item 2");
    checkedListBox1.Items.Add("Item 3");
    checkedListBox1.Items.Add("Item 4");
    checkedListBox1.Items.Add("Item 5");
}

In this example, we add five items to the CheckBoxList control. When the application is run, these items will appear on the form, each with a corresponding checkbox.

Now, we need to add some event handlers to the CheckBoxList control to get notified when the user selects an item. The CheckBoxList control has two related events: ItemCheck and SelectedIndexChanged. The ItemCheck event fires when the user clicks a checkbox, and the SelectedIndexChanged event fires when the user selects an item. You can choose to use one or both events. Here is an example using the ItemCheck event:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    
    
    if (e.NewValue == CheckState.Checked)
    {
    
    
        MessageBox.Show("您选择了 " + checkedListBox1.Items[e.Index].ToString());
    }
    else
    {
    
    
        MessageBox.Show("您取消了选择 " + checkedListBox1.Items[e.Index].ToString());
    }
}

In this example, we pop up a message box when the user checks or unchecks an item. A message box displays the text of items selected or deselected by the user. To get the index of the selected item, we can use the Index property of the ItemCheckEventArgs object. To get the text of the selected item, we can use the Items collection of the CheckBoxList control.

Finally, we can also use the CheckedItems property to get all the items selected by the user. Here is an example:

private void button1_Click(object sender, EventArgs e)
{
    
    
    string selectedItems = "";
    foreach (var item in checkedListBox1.CheckedItems)
    {
    
    
        selectedItems += item.ToString() + Environment.NewLine;
    }
    MessageBox.Show("您选择了以下项目: " + Environment.NewLine + selectedItems);
}

In this example, we use a loop to iterate through the CheckedItems collection and add the text of the selected item to a string. Finally, we pop up a message box that displays the text of all items selected by the user.

This is a very basic example showing how to use the CheckBoxList control in a Winforms application. You can also use other properties and methods, such as CheckedIndices, SetItemChecked, and GetItemCheckState, for more advanced functionality.

insert image description here

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/132667440