Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CheckedListBox Control in C #

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use CheckedListBox controls in C#. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Common properties, methods and events 1. Common attribut

(1) CheckOnClick attribute

The CheckOnClick attribute is a Boolean value, and if it is True, click the entry to check it; if it is false, double-click the entry to check it.

(2) ColumnWidth attribute

The ColumnWitdh property is integer data that represents the column width of each column of a multi-column entry. This property is meaningful only if the MultiColumn property (which allows multiple columns to be displayed) is set to true.

Multiple columns are displayed as shown below:

(3) MultiColumn attribute

The MultiColumn property is a Boolean value indicating whether multi-column display entries are turned on. This property is used with the ColumnWidth property.

(4) SelectMode attribute

This attribute indicates whether the list will be single or multiple, where "select" means selected instead of "check", the item will be highlighted, but the small box to the left of the entry will not be checked. Multiple selections are not supported in the CheckedLIstBox control, and only two values for this property are valid, None and One. None means that items cannot be checked, 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).

(5) Sorted attribute

This attribute is the value of the Boolean center, and if true, the entries are sorted alphabetically, if false, not sorted.

(6) Count attribute

This attribute represents the total number of items in the list. The usage is as follows:

Int conut=checkedListBox1.Items.Count

(7) Items attribute

Gets the collection of items in the list and gets the specified entry by subscript.

Object item=checkedListBox1.Items [i]

(8) SelectedItem attribute and SelectedItems attribute

The function of the SelectedItem attribute is to get the selected items, while the SelectedItems attribute is an array that holds the collection of selected items, which can be obtained by subscripts. 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. The usage is as follows:

CheckedListBox1.Items.Add ("item one"); / / the parameter can be a string or an entry or checkedListBox1.Items.Add ("item one", true); / / the second parameter is of Boolean type, indicating whether to let the entry be checked.

(2) RemoveAt method

This method is used to remove the specified entry, and the parameter is the index value of the entry. The usage is as follows:

CheckedListBox1.Items.RemoveAt (1)

(3) Insert method

The purpose of this method is to insert an entry at the specified location, with two parameters, index and item. Index is the index of an entry, and item can be either an entry or a character scramble. Its usage is as follows:

CheckedListBox1.Items.Insert (I, item one)

(4) GetItemChecked method

This method returns whether item I is checked (not checked), true if so, false otherwise. Parameter is the index value of the entry. Its usage is as follows:

Bool isChecked=checkedListBox1.GetItemChecked (I)

(5) SetItemChecked method

To set whether item I is selected, there are two parameters, the first is the index and the second is the Boolean value. The second parameter, if true is selected, sets item I to check, otherwise it is unchecked. Its usage is as follows:

CheckedListBox1.SetItemChecked (iMagneTrue)

(6) Clear method

The purpose of this method is to clear all entries. Its usage is as follows:

CheckedListBox1.Clear (); 3. Common events

(1) ItemCheck event

Occurs when an item is checked, and its usage is as follows:

Private void checkedListBox1_ItemCheck (object sender, ItemCheckEventArgs e) {/ / entries checked by index reference textBox1.Text = textBox1.Text + "\ r\ n" + checkedListBox1.Items [e.Index] .ToString ();}

(2) SelectedIndexChanged and SelectedValuedChanged events

Both events occur when the item is selected (not necessarily checked)

Second, application example 1. Case description

Use CheckedBox and CheckedListBox to achieve the functions shown in the following figure: click the check box to check the coordinates, and an entry will be added to the CheckedLIstBox of the edge, and the entry will be displayed in multiple columns. Click to check the entry in the CheckedLIstBox on the right, and the contents of the entry will be displayed in the text box below.

two。 Property settin

In the property bar, set the MultiColumn property of checkedListBox1 to true to achieve multi-column display of items

3. Event handling

(1) CheckedChanged event of checkedBox1

First, customize a method to handle the CheckedChanged event of checkedBox1:

Private void CheckedChanged (object sender,EventArgs e) {CheckBox cb = (CheckBox) sender;//sender is the checked CheckedBox. Cast to CheckedBox type if (cb.Checked) {checkedListBox1.Items.Add (cb.Text);} before referencing.

Then, in the property bar of CheckedBox, select CheckedChanged for all CheckedBox's CheckedChanged event handling methods, so that multiple check boxes can share a single event handling method. As shown in the figure:

(2) ItemCheck event of checkedListBox1

4. Implementation code

Thank you for reading! This is the end of this article on "how to use CheckedListBox controls in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report