Click here to Skip to main content
15,861,168 members
Articles / Desktop Programming / MFC
Article

CheckComboBox Control

Rate me:
Please Sign up or sign in to vote.
4.81/5 (55 votes)
24 Nov 19992 min read 514.3K   15.8K   121   109
A combo box with check boxes.

Sample Image

Introduction

What does a CheckComboBox control do? A combo box has always been a way of selecting one item from a list of several. If you need to select one or more items, you have to either use a multiple selection listbox or use the MFC's CCheckListBox. The only problem is that it takes up space in your dialog or form. What you need is a CCheckComboBox!

When I started to develop the control, I was planning to design a composite control consisting of a static control along with a small button. When clicking the button, a popup window would be displayed where the user could select or unselect the items. The problem was that it took too much code to implement. I wanted the look and feel just like a standard combo box and that was not possible with a reasonable amount of code.

So I decided to subclass the combo box. A problem when subclassing the combo box is that you have no window handle to the list portion of the combo box that you may subclass. You only have the combo box window handle and that doesn't help.

The trick is to listen to the WM_CTLCOLORLISTBOX message. When the WM_CTLCOLORLISTBOX message is sent, lParam contains the window handle of the list box. This is the time to do the subclassing.

The code below shows how this is done:

BEGIN_MESSAGE_MAP(CCheckComboBox, CComboBox)
    ...
    ON_MESSAGE(WM_CTLCOLORLISTBOX, OnCtlColorListBox)
    ...
END_MESSAGE_MAP()

...
LRESULT CCheckComboBox::OnCtlColorListBox(WPARAM wParam, LPARAM lParam) 
{
    // If the listbox hasn't been subclassed yet, do so...
    if (m_hListBox == 0) {
        HWND hWnd = (HWND)lParam;

        if (hWnd != 0 && hWnd != m_hWnd) {
           // Save the listbox handle
           m_hListBox = hWnd;

           // Do the subclassing
           m_pWndProc = (WNDPROC)GetWindowLong(m_hListBox, GWL_WNDPROC);
           SetWindowLong(m_hListBox, GWL_WNDPROC, (LONG)ComboBoxListBoxProc);
        }
    }


    return DefWindowProc(WM_CTLCOLORLISTBOX, wParam, lParam);
}

After this is done, the rest is fairly straight forward. The ComboBoxListBoxProc() routine takes care of selecting/unselecting items and key down events. The overridden DrawItem() routine draws both the list items and the static portion of the combo box. If it is an item, it draws a checkmark followed by the text. If it is the static portion, it draws the text from all selected items separated by the standard list separator.

Please note that the CCheckComboBox must be created with the CBS_DROPDOWNLIST and the CBS_OWNERDRAWVARIABLE styles set. Also the CBS_HASSTRINGS style must be specified since I let the combo box handle the item strings (sorting etc.).

Since the check mark information is stored in the combo box item data, this may not be used by the application. Setting it to zero unselects the item otherwise it selects the item. But use the GetCheck() and SetCheck() routines instead since they handle redrawing correctly. If you need your own item data, you must implement this yourself.

It is easy to use this control since it is derived from the MFC CComboBox. Add CheckComboBox.h and CheckComboBox.cpp to your project and use it as a standard CComboBox. When used inside a dialog, simply select the category "Control" and variable type "CCheckComboBox" under the "Member Variables" page of the Class Wizard. This will declare a member variable in your dialog of type CCheckComboBox ready for you to use.

Well, I hope someone finds this useful. At least I did.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCant see checkbox icons Pin
Member 1140264617-Apr-18 5:55
Member 1140264617-Apr-18 5:55 
QuestionLicense information is missing Pin
BHxx7-Feb-17 2:05
BHxx7-Feb-17 2:05 
QuestionLicense info Pin
Member 117849145-Dec-16 2:18
Member 117849145-Dec-16 2:18 
BugDoes not display text with ampersand e.g. "Draw&Text" correctly because of missing DT_NOPREFIX flag in call DrawText Pin
User 2694230-Jun-16 22:30
professionalUser 2694230-Jun-16 22:30 
QuestionLicensing information missing... Pin
Member 1240925122-Mar-16 1:37
Member 1240925122-Mar-16 1:37 
AnswerRe: Licensing information missing... Pin
BHxx24-Nov-16 23:20
BHxx24-Nov-16 23:20 
BugUnicode problem Pin
User 269424-Nov-13 22:50
professionalUser 269424-Nov-13 22:50 
GeneralRe: Unicode problem Pin
Anurag Daware31-May-15 21:01
Anurag Daware31-May-15 21:01 
GeneralRe: Unicode problem Pin
User 2694230-Jun-16 22:18
professionalUser 2694230-Jun-16 22:18 
QuestionCCheckComboBox II & VB.net Pin
Eran SMS11-Jul-13 1:33
Eran SMS11-Jul-13 1:33 
QuestionUnable to increase drop down wdith Pin
NipunG25-Sep-12 20:44
NipunG25-Sep-12 20:44 
AnswerRe: Unable to increase drop down wdith Pin
Anurag Daware31-May-15 20:39
Anurag Daware31-May-15 20:39 
QuestionCBN_SELCHANGE? Pin
bosfan31-Jul-12 1:31
bosfan31-Jul-12 1:31 
BugWin x64 fail Pin
AJG8514-May-12 13:13
AJG8514-May-12 13:13 
GeneralRe: Win x64 fail Pin
AJG8515-May-12 11:40
AJG8515-May-12 11:40 
QuestionHow to use GetDlgItem() Pin
amitwadekar17-Jan-12 2:08
amitwadekar17-Jan-12 2:08 
GeneralMy vote of 5 Pin
Emmo2-Jun-11 5:20
Emmo2-Jun-11 5:20 
GeneralMy vote of 5 Pin
Phil Outram6-Aug-10 5:00
Phil Outram6-Aug-10 5:00 
GeneralMy vote of 1 Pin
xinkmt3-Aug-10 15:09
xinkmt3-Aug-10 15:09 
GeneralBug - before subclassing behavior Pin
John59027-Aug-09 0:37
professionalJohn59027-Aug-09 0:37 
I have found a little but annying bug.
If you start the demo and only press the tab key to reach the combo box and then you press the UP or DOWN keys then the combo shows the checkbox and test of the currently selected item instead of the checked items list.

To fix this annoying problem you only need to correct this line:
// Check if we are drawing the static portion of the combobox
if( (LONG)lpDrawItemStruct->itemID < 0 ) {

TO

// Check if we are drawing the static portion of the combobox
if( (LONG)lpDrawItemStruct->itemID < 0 || m_hListBox == 0 ) {

I hope this help all those that are using this class.
GeneralRe: Bug - before subclassing behavior...and fix to get arrow keys opening drop down box Pin
Phil Outram6-Aug-10 5:23
Phil Outram6-Aug-10 5:23 
GeneralExcellent control Pin
simbakid26-Mar-09 22:14
simbakid26-Mar-09 22:14 
Questioncombobox show only one item at dropdown Pin
rotalume17-Jan-09 6:16
rotalume17-Jan-09 6:16 
AnswerRe: combobox show only one item at dropdown Pin
rotalume18-Jan-09 18:55
rotalume18-Jan-09 18:55 
QuestionCan you give more explain? Pin
paradise of pig20-Oct-08 8:35
paradise of pig20-Oct-08 8:35 

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.