Click here to Skip to main content
13,300,002 members (61,202 online)
Click here to Skip to main content
Add your own
alternative version


97 bookmarked
Posted 20 Nov 2008

A ComboBox with a CheckedListBox as a Dropdown

, 22 Nov 2008
Rate this:
Please Sign up or sign in to vote.
A ComboBox with a CheckedListBox as a dropdown


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).


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:

  1. display the dropdown list, and
  2. prevent any keyboard input, in order to make the text portion read-only.
protected override void OnDropDown(EventArgs e) {

private void DoDropDown() {
    if (!dropdown.Visible) {
        Rectangle rect = RectangleToScreen(this.ClientRectangle);
        dropdown.Location = new Point(rect.X, rect.Y + this.Size.Height);
        int count = dropdown.List.Items.Count;
        if (count > this.MaxDropDownItems) {
            count = this.MaxDropDownItems;
        } else if (count == 0) {
            count = 1;
        dropdown.Size = new Size(this.Size.Width, 
                       (dropdown.List.ItemHeight + 1) * count);

protected override void OnKeyDown(KeyEventArgs e) {
    if (e.KeyCode == Keys.Down) {        
    // Make sure that certain keys or combinations are not blocked.
    e.Handled = !e.Alt && !(e.KeyCode == Keys.Tab) &&
        !((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right) || 
    (e.KeyCode == Keys.Home) || (e.KeyCode == Keys.End));

protected override void OnKeyPress(KeyPressEventArgs e) {
    e.Handled = true;

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).

protected override void OnDeactivate(EventArgs e) {
    CCBoxEventArgs ce = e as CCBoxEventArgs;
    if (ce != null) {
    } else {


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 = ", ";

In order to add items, use the Items collection...

private string[] coloursArr = { "Red", "Green", "Black", 
                                "White", "Orange", "Yellow", 
                                "Blue", "Maroon", "Pink", "Purple" };

for (int i = 0; i < coloursArr.Length; i++) {
    CCBoxItem item = new CCBoxItem(coloursArr[i], i);

... where the CCBoxItem is a simple test class I've created as follows (it's in the demo):

public class CCBoxItem {
    private int val;
    public int Value {
        get { return val; }
        set { val = value; }
    private string name;
    public string Name {
        get { return name; }
        set { name = value; }
    public CCBoxItem() {
    public CCBoxItem(string name, int val) { = name;
        this.val = val;
    public override string ToString() {
        return string.Format("name: '{0}', value: {1}", name, val);

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.

// Add the handlers to the CheckedComboBox

this.ccb.DropDownClosed += new System.EventHandler(this.ccb_DropDownClosed);
ccb.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.ccb_ItemCheck);

// Handler implementation

private void ccb_DropDownClosed(object sender, EventArgs e) {
    txtOut.AppendText(string.Format("value changed: {0}\r\n", ccb.ValueChanged));
    txtOut.AppendText(string.Format("value: {0}\r\n", ccb.Text));
    // Display all checked items.
    StringBuilder sb = new StringBuilder("Items checked: ");
    // Checked items can be found via the CheckedItems property.
    foreach (CCBoxItem item in ccb.CheckedItems) {
    sb.Remove(sb.Length-ccb.ValueSeparator.Length, ccb.ValueSeparator.Length);

private void ccb_ItemCheck(object sender, ItemCheckEventArgs e) {
    CCBoxItem item = ccb.Items[e.Index] as CCBoxItem;
    txtOut.AppendText(string.Format("Item '{0}' is about to be {1}\r\n", 
                                     item.Name, e.NewValue.ToString()));

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.


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)


    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)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Stelios Alexandrakis
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to fix having to click twice Pin
Herrpel9-May-17 7:59
memberHerrpel9-May-17 7:59 
AnswerRe: How to fix having to click twice Pin
Philippe Lamontagne13-Jul-17 5:18
memberPhilippe Lamontagne13-Jul-17 5:18 
QuestionForm loses on top Pin
MatteoPev30-Apr-17 1:18
memberMatteoPev30-Apr-17 1:18 
AnswerRe: Form loses on top Pin
MatteoPev30-Apr-17 1:32
memberMatteoPev30-Apr-17 1:32 
GeneralRe: Form loses on top Pin
chaosanarchie7-Jul-17 2:02
memberchaosanarchie7-Jul-17 2:02 
AnswerRe: Form loses on top Pin
Member 97829382-Sep-17 9:54
memberMember 97829382-Sep-17 9:54 
QuestionFirst Click not working Pin
Chinmaya Dash3-Apr-17 0:53
memberChinmaya Dash3-Apr-17 0:53 
AnswerRe: First Click not working Pin
anlo284622-Sep-17 2:02
memberanlo284622-Sep-17 2:02 
QuestionHaving to click twice Pin
Member 1275358924-Mar-17 4:38
memberMember 1275358924-Mar-17 4:38 
AnswerRe: Having to click twice Pin
anlo284622-Sep-17 2:03
memberanlo284622-Sep-17 2:03 
QuestionIn those times of WPF/UWP, Angular, ... this Windows Forms control is very helpful Pin
Member-79325584-Mar-17 17:12
memberMember-79325584-Mar-17 17:12 
QuestionClear Item Pin
Member 1268564517-Dec-16 16:30
memberMember 1268564517-Dec-16 16:30 
AnswerRe: Clear Item Pin
Member 1295668018-Jan-17 9:54
memberMember 1295668018-Jan-17 9:54 
AnswerRe: Clear Item Pin
anlo284622-Sep-17 2:09
memberanlo284622-Sep-17 2:09 
QuestionMulticolumn? Pin
DerrellGore20-Sep-16 12:08
memberDerrellGore20-Sep-16 12:08 
AnswerRe: Multicolumn? Pin
Stelios Alexandrakis22-Sep-16 3:20
memberStelios Alexandrakis22-Sep-16 3:20 
GeneralRe: Multicolumn? Pin
DerrellGore22-Sep-16 10:53
memberDerrellGore22-Sep-16 10:53 
GeneralRe: Multicolumn? Pin
Stelios Alexandrakis22-Sep-16 16:15
memberStelios Alexandrakis22-Sep-16 16:15 
GeneralRe: Multicolumn? Pin
DerrellGore22-Sep-16 16:49
memberDerrellGore22-Sep-16 16:49 
QuestionA ComboBox with a CheckedListBox as a Dropdown Pin
Member 830449519-May-16 21:04
memberMember 830449519-May-16 21:04 
AnswerRe: A ComboBox with a CheckedListBox as a Dropdown Pin
Stelios Alexandrakis22-May-16 1:51
memberStelios Alexandrakis22-May-16 1:51 
QuestionRe: A ComboBox with a CheckedListBox as a Dropdown Pin
Member 830449522-May-16 19:30
memberMember 830449522-May-16 19:30 
AnswerRe: A ComboBox with a CheckedListBox as a Dropdown Pin
Stelios Alexandrakis26-May-16 14:57
memberStelios Alexandrakis26-May-16 14:57 
GeneralAcknowledgement Pin
Member 1253190518-May-16 12:43
memberMember 1253190518-May-16 12:43 
GeneralRe: Acknowledgement Pin
Stelios Alexandrakis22-May-16 1:52
memberStelios Alexandrakis22-May-16 1:52 
PraiseThanks dude... Pin
Digambar Malla29-Jan-16 1:42
memberDigambar Malla29-Jan-16 1:42 
GeneralRe: Thanks dude... Pin
Stelios Alexandrakis22-May-16 1:52
memberStelios Alexandrakis22-May-16 1:52 
QuestionHave anyone Successfully Converted this piece of wonderful code to Vb.Net Pin
Divyang Patel5-Oct-15 12:24
memberDivyang Patel5-Oct-15 12:24 
QuestionNeed CheckedListBox Focused Pin
anelephant2-Jul-15 0:13
memberanelephant2-Jul-15 0:13 
QuestionRe: Need CheckedListBox Focused Pin
juvvadi28-Aug-15 4:25
memberjuvvadi28-Aug-15 4:25 
AnswerRe: Need CheckedListBox Focused Pin
langjacques17-Sep-15 1:47
memberlangjacques17-Sep-15 1:47 
QuestionAutocomplete? Pin
Darren Schroeder5-Feb-15 5:02
memberDarren Schroeder5-Feb-15 5:02 
Questioncouple of bugs Pin
billstocktonbillstockton27-Nov-14 14:28
memberbillstocktonbillstockton27-Nov-14 14:28 
AnswerRe: couple of bugs Pin
Stelios Alexandrakis22-May-16 2:22
memberStelios Alexandrakis22-May-16 2:22 
QuestionHi I need Select All Check option in this. Pin
VigneshKumar9579-Oct-14 18:37
memberVigneshKumar9579-Oct-14 18:37 
AnswerRe: Hi I need Select All Check option in this. Pin
Stelios Alexandrakis22-May-16 2:21
memberStelios Alexandrakis22-May-16 2:21 
GeneralMy vote of 1 Pin
Member 110218046-Oct-14 2:34
memberMember 110218046-Oct-14 2:34 
QuestionConversion From C# to VB.NET Pin
fluffy314-Sep-14 3:58
memberfluffy314-Sep-14 3:58 
QuestionCombobox is not closed directly. Need to click twise at out side. Pin
dhaval.developer11-Aug-14 22:28
memberdhaval.developer11-Aug-14 22:28 
QuestionHow to call items.clear ? Pin
dhaval.developer11-Aug-14 22:27
memberdhaval.developer11-Aug-14 22:27 
QuestionGetting erro when coverted to Please guide, bit urgent for me. Pin
dhaval.developer11-Aug-14 7:17
memberdhaval.developer11-Aug-14 7:17 
GeneralRe: Getting erro when coverted to Please guide, bit urgent for me. Pin
PIEBALDconsult11-Aug-14 7:27
memberPIEBALDconsult11-Aug-14 7:27 
GeneralRe: Getting erro when coverted to Please guide, bit urgent for me. Pin
dhaval.developer11-Aug-14 20:44
memberdhaval.developer11-Aug-14 20:44 
GeneralRe: Getting erro when coverted to Please guide, bit urgent for me. Pin
dhaval.developer11-Aug-14 22:22
memberdhaval.developer11-Aug-14 22:22 
Questionhow to use this combobox with Valuemember and Displaymember Pin
duonglei16-Jun-14 17:47
memberduonglei16-Jun-14 17:47 
AnswerRe: how to use this combobox with Valuemember and Displaymember Pin
Member 113756725-Jan-16 7:29
memberMember 113756725-Jan-16 7:29 
QuestionProblem with one item. Pin
Member 24731043-Jun-14 2:26
memberMember 24731043-Jun-14 2:26 
Question.SelectionChangeCommitted Pin
Freudi_t29-Apr-14 0:05
memberFreudi_t29-Apr-14 0:05 
QuestionThanks! Pin
Member 1065857711-Mar-14 14:19
memberMember 1065857711-Mar-14 14:19 
QuestionRightToLeft Pin
me&he8-Jan-14 13:45
memberme&he8-Jan-14 13:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 22 Nov 2008
Article Copyright 2008 by Stelios Alexandrakis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid