Home > Backend Development > C#.Net Tutorial > Summary of how to use the CheckedListBox control in C#

Summary of how to use the CheckedListBox control in C#

WBOY
Release: 2024-02-19 10:23:06
Original
1263 people have browsed it

Summary of how to use the CheckedListBox control in C#

Summary of usage of C#'s CheckedListBox control

CheckedListBox is one of the commonly used controls in C# Windows Forms. It is used to display a set of optional options, and the user can check the Marquee selects one or more options. In this article, we will summarize the usage of the CheckedListBox control and provide some specific code examples.

1. Basic usage of CheckedListBox

  1. Create CheckedListBox control
    To use the CheckedListBox control, you first need to create a CheckedListBox instance on the form. You can create them by dragging and dropping controls in Visual Studio's designer, or by instantiating objects in code.
CheckedListBox checkedListBox1 = new CheckedListBox();
Copy after login
  1. Add options
    Options can be added to the CheckedListBox through the Items property.
checkedListBox1.Items.Add("选项1");
checkedListBox1.Items.Add("选项2");
checkedListBox1.Items.Add("选项3");
Copy after login
  1. Get the selected options
    To get the options selected by the user, you can use the CheckedIndices attribute or CheckedItems attribute. The CheckedIndices property returns a collection of indices of selected items, and the CheckedItems property returns a collection of selected items.
// 获取选中项的索引
for (int i = 0; i < checkedListBox1.CheckedIndices.Count; i++)
{
    int selectedIndex = checkedListBox1.CheckedIndices[i];
}

// 获取选中项的值
foreach (var item in checkedListBox1.CheckedItems)
{
    string selectedValue = item.ToString();
}
Copy after login
  1. Listening to option change events
    When the user checks or unchecks an option, you can respond through the CheckedChanged event.
checkedListBox1.CheckedChanged += CheckedListBox1_CheckedChanged;

private void CheckedListBox1_CheckedChanged(object sender, EventArgs e)
{
    // 处理选项改变的逻辑
}
Copy after login

2. Advanced usage of CheckedListBox

  1. Set the default selected item
    You can use the SetItemChecked method to set the default selected item. This method has two parameters, the first parameter is the index of the option, and the second parameter is a Boolean value indicating whether it is selected.
checkedListBox1.SetItemChecked(0, true); // 默认选中第一个选项
Copy after login
  1. Data binding
    CheckedListBox also supports data binding. You can bind the data source and display fields to the control through the DataSource property and DisplayMember property.
List<string> dataList = new List<string> { "选项1", "选项2", "选项3" };
checkedListBox1.DataSource = dataList;
Copy after login
  1. Modify option style
    You can modify the option style, such as font, color, etc., through the ItemCheck event.
checkedListBox1.ItemCheck += CheckedListBox1_ItemCheck;

private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    // 修改选项样式
    if (e.NewValue == CheckState.Checked)
    {
        checkedListBox1.SetItemChecked(e.Index, true);
        checkedListBox1.SetItemCheckState(e.Index, CheckState.Indeterminate);
    }
}
Copy after login
  1. Multi-column display
    By setting the CheckOnClick property of CheckedListBox to true, you can achieve the effect of multi-column display.
checkedListBox1.CheckOnClick = true;
Copy after login

Summary:
The above are the basic usage and some advanced usage of the CheckedListBox control. By using the CheckedListBox control, you can easily implement the multi-selection function and customize it according to actual needs. I hope this article can help you better understand and use the CheckedListBox control.

The above is the detailed content of Summary of how to use the CheckedListBox control in C#. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template