CheckedListBox Control

The CheckedListBox control derives from class ListBox and includes a CheckBox next to each item. As in ListBoxes, items can be added via methods Add and AddRange or through the String Collection Editor. CheckedListBoxes imply that multiple items can be selected, and the only possible values for the SelectionMode property are None and One. One allows multiple selection, because CheckBoxes imply that there are no logical restrictions on the itemsthe user can select as many items as required. Thus, the only choice is whether to give the user multiple selection or no selection at all. This keeps the CheckedListBox's behavior consistent with that of CheckBoxes. Common properties, events and methods of CheckedListBoxes appear in Fig. 14.19.

Figure 14.19. CheckedListBox properties, methods and events.

(This item is displayed on page 672 in the print version)

CheckedListBox properties, methods and events


Common Properties

(All the ListBox properties, methods and events are inherited by CheckedListBox.)


Contains the collection of items that are checked. This is distinct from the selected item, which is highlighted (but not necessarily checked). [Note: There can be at most one selected item at any given time.]


Returns indices for all checked items.


Determines how many items can be checked. The only possible values are One (allows multiple checks to be placed) or None (does not allow any checks to be placed).

Common Method


Takes an index and returns true if the corresponding item is checked.

Common Event (Event arguments ItemCheckEventArgs)


Generated when an item is checked or unchecked.

ItemCheckEventArgs Properties


Indicates whether the current item is checked or unchecked. Possible values are Checked, Unchecked and Indeterminate.


Returns the zero-based index of the item that changed.


Specifies the new state of the item.

Common Programming Error 14 1

The IDE displays an error message if you attempt to set the SelectionMode property to MultiSimple or MultiExtended in the Properties window of a CheckedListBox. If this value is set programmatically, a runtime error occurs.

Event ItemCheck occurs whenever a user checks or unchecks a CheckedListBox item. Event argument properties CurrentValue and NewValue return CheckState values for the current and new state of the item, respectively. A comparison of these values allows you to determine whether the CheckedListBox item was checked or unchecked. The CheckedListBox control retains the SelectedItems and SelectedIndices properties (it inherits them from class ListBox). However, it also includes properties CheckedItems and CheckedIndices, which return information about the checked items and indices.

In Fig. 14.20, class CheckedListBoxTestForm uses a CheckedListBox and a ListBox to display a user's selection of books. The CheckedListBox allows the user to select multiple titles. In the String Collection Editor, items were added for some Deitel books: C++, Java™, Visual Basic, Internet & WWW, Perl, Python, Wireless Internet and Advanced Java (the acronym HTP stands for "How to Program"). The ListBox (named displayListBox) displays the user's selection. In the screenshots accompanying this example, the CheckedListBox appears to the left, the ListBox on the right.

Figure 14.20. CheckedListBox and ListBox used in a program to display a user selection.

 1 // Fig. 14.20: CheckedListBoxTestForm.cs
 2 // Using the checked ListBox to add items to a display ListBox
 3 using System;
 4 using System.Windows.Forms;
 6 // Form uses a checked ListBox to add items to a display ListBox
 7 public partial class CheckedListBoxTestForm : Form
 8 {
 9 // default constructor
10 public CheckedListBoxTestForm()
11 {
12 InitializeComponent();
13 } // end constructor
15 // item about to change
16 // add or remove from display ListBox
17 private void inputCheckedListBox_ItemCheck(
18 object sender, ItemCheckEventArgs e )
19 {
20 // obtain reference of selected item
21 string item = inputCheckedListBox.SelectedItem.ToString();
23 // if item checked add to ListBox
24 // otherwise remove from ListBox
25 if ( e.NewValue == CheckState.Checked )
26 displayListBox.Items.Add( item );
27 else
28 displayListBox.Items.Remove( item );
29 } // end method inputCheckedListBox_ItemCheck
30 } // end class CheckedListBoxTestForm





When the user checks or unchecks an item in inputCheckedListBox, an ItemCheck event occurs and event handler inputCheckedListBox_ItemCheck (lines 1729) executes. An if...else statement (lines 2528) determines whether the user checked or unchecked an item in the CheckedListBox. Line 25 uses the NewValue property to determine whether the item is being checked (CheckState.Checked). If the user checks an item, line 26 adds the checked entry to the ListBox displayListBox. If the user unchecks an item, line 28 removes the corresponding item from displayListBox. This event handler was created by selecting the CheckedListBox in Design mode, viewing the control's events in the Properties window and double clicking the ItemCheck event.



    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look


    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2


    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures



    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types


    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600 © 2008-2020.
    If you may any questions please contact us: