Home
Search
CheckedListBox: Windows FormsAdd the CheckedListBox control, which shows a list of items. Access SelectedIndex.
C#
This page was last reviewed on Sep 29, 2022.
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.
Getting started. 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.
Example code. 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.
Here In this first example, you need to add the Form1_Load event handler by double-clicking on the form as well.
Event
Info You can add items to the 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.
Tip You must check the SelectedIndex property for the value -1 to prevent an IndexOfOutRangeException.
IndexOutOfRangeException
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(); } } } }
Items. 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.
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 29, 2022 (edit).
Home
© 2007-2022 sam allen.
see site info on the changelog.