Introduction

This control came out of my need to create a CheckedListBox control which does not occupy as much space on the form while the selected item(s) are visible. Although a few examples already exist using owner-drawn controls (i.e., drawing the ListBox portion of the ComboBox), I wasn't fully satisfied because of some shortcomings, hence I came up with a new version, the CheckedComboBox which has the following features:

Read-only text portion, displaying the checked item(s).

Each checked item appears in the text portion as a string, each string separated by a custom separator that I may define.

List portion stays open until the user finishes the selection.

Check/uncheck all items.

Display/hide the list portion via the keyboard.

Mimic as closely as possible the .NET ComboBox.

Use it from the Visual Studio palette (as a custom control).

Implementation

The CheckedComboBox derives from ComboBox. In order to avoid the problem of the list portion disappearing before the user finishes the selection, I decided to catch the DropDown event and display my own list portion which consists of a Form whose client area is completely filled by a CheckListBox. Both the Form and the CheckListBox are my own custom versions since I needed to override some of the default behaviour.

Therefore, my custom list portion appears either on the DropDown event or when the user presses the Down arrow while the CheckedComboBox has the focus. I overrode the OnDropDown(), OnKeyDown(), and the OnKeyPress() methods to:

display the dropdown list, and

prevent any keyboard input, in order to make the text portion read-only.

Now, the list portion, being a Form, will stay open without being closed (hidden actually) until the user either presses Esc (the Escape button) to cancel any changes, Enter to accept all changes, or clicks with the mouse anywhere outside the list, which is the usual behaviour of the ComboBox.

In order to achieve this, I had to catch the Deactivate event of the Form (for the mouse click behaviour) and the keyboard events (for the Esc, Enter behaviour). In addition, if the user presses Del (the Delete button) or Shift + Del while the list portion has the focus, all items will be unchecked/checked, respectively, allowing a useful (to me, anyway :-)) keyboard shortcut. Note how a custom CCBoxEventArgs class has been used in order to distinguish whether the Deactivate message comes from the framework (i.e., the mouse click) or from the keyboard (to which I have control since I'm trapping keystrokes).

Using the Code

The code comes as a demo project which you can run and see as an example of usage. In order to use the CheckedComboBox in your code, you only need one file, CheckedComboBox.cs. Although I have not made it a fully-functional Visual Studio control with all the bows and whistles, once you compile your project, the CheckedComboBox should appear under the Components in your Visual Studio ToolBox palette and you can drag and drop it on to your Form in the Designer.

Alternatively, you can very easily use it by manually writing code like shown below to declare and create an instance of a CheckedComboBox:

private CheckedComboBox ccb = new CheckedComboBox();
// If more than 5 items, add a scroll bar to the dropdown.
ccb.MaxDropDownItems = 5;
// Make the "Name" property the one to display, rather than the ToString()
// representation of the item.
ccb.DisplayMember = "Name";
// Set a separator for how the checked items will appear in the Text portion.
ccb.ValueSeparator = ", ";

You can also programmatically check an item or set its CheckState to Checked, Unchecked or Indeterminate. Note that the user cannot set an item state to Indeterminate from the User Interface, it can only be programmatically set (unless you want to handle the ItemCheck notification and add your own logic there).

// Check first item (index == 0)
ccb.SetItemChecked(0, true);
// Set the CheckState of the 2nd item in the list to Indeterminate.
ccb.SetItemCheckState(1, CheckState.Indeterminate);

The most interesting events you would probably be catching in your application are when the drop down portion closes (DropDownClosed) and when an item checked state is about to change (ItemCheck). You can register event handlers and handle the events as usual.

Note that the DropDownClosed event is available from the ComboBox directly, but the ItemCheck I had to make available from the CheckListBox.

In the code above, you can see that the checked items are available via the CheckedItems property. Similarly, CheckedIndices will return a collection of checked indices. Both these collections correspond to the .NET CheckedListBox collection, so nothing unfamiliar here.

History

In version 2.0, I have included some minor fixes/enhancements as follows:

Exposed the SetItemChecked(int index, bool isChecked) method so that you can programmatically set an item as checked

Relevant to the above, also included two more methods to get/set the CheckState:

CheckState GetItemCheckState(int index)

and

SetItemCheckState(int index, CheckState state)

Added a horizontal scrollbar to the dropdown list, which becomes visible if any of the items' length exceeds the width of the dropdownlist

Made the dropdown list "invisible" from the task bar (it used to appear because it is a Form)

Comments and Discussions

Hello stelios,This ia a Great control and its more useful in places where there is aneed to fit many options in a Limited screen area.I have a small thing i.e i like to have these list box items in 2 or morerows. Can you suggest me any option to implement this.

Multi Columns can be obtained when this.MultiColumn = true; is writtenin the constructor of CustomCheckedListBox class of the control source code. Even column width can also be set here like, this.ColumnWidth = 60;

But a small thing i like to have the option for the user to edit these options from the properties window. Can any one tell me how do i Implement this.