CheckedListBox
This control presents several items in a list. Each item is checkable—the user can check a box. The CheckedListBox
control is a way to make longer, dynamic checkable lists.
Please create a Windows Forms program in Visual Studio. Next, please open the Toolbox and double-click on the CheckedListBox
item. A new control is created.
We are using the C# language here. You can right-click on the CheckedListBox
and select Properties to adjust properties and also add event handlers.
Form1_Load
event handler by double-clicking on the form as well.CheckedListBox
at design time. But you can also add items dynamically at runtime.using System; using System.Windows.Forms; namespace WindowsFormsApplication25 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { var items = checkedListBox1.Items; items.Add("Perls"); items.Add("Checked", true); } } }
SelectedIndex
Another useful event handler is the SelectedIndexChanged
handler. You might use this to display some text in the UI when an item is selected.
SelectedIndex
property for the value -1 to prevent an IndexOfOutRangeException
.using System; using System.Windows.Forms; namespace WindowsFormsApplication25 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) { // Get selected index, and then make sure it is valid. int selected = checkedListBox1.SelectedIndex; if (selected != -1) { this.Text = checkedListBox1.Items[selected].ToString(); } } } }
It is also possible to edit the Items at design time. In Visual Studio, select Properties and then Items on the CheckedListBox
instance.
CheckOnClick
When CheckOnClick
is False, the user must click a second time after an item is selected to check the box. When True, the user must only click once to check a box.
IntegralHeight
It is possible to use the IntegralHeight
property on the CheckedListBox
to ensure that no partial items are shown.
Enabled
With the CheckedListBox
, setting Enabled
to False will gray out all the items in the control. It will also make them unclickable and unselectable.
ThreeDCheckBoxes
By default, the CheckedListBox
has checkboxes that have a flat, single-pixel border. A more 3D appearance may be desirable—set the ThreeDCheckBoxes
property to true.
CheckedListBox
is useful for displaying a large or dynamic set of items that can be checked by the user. It has a many properties that adjust appearance and behavior.