5,276,156 members and growing! (16,220 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » ComboBox Controls     Intermediate

CheckComboBox Control

By Magnus Egelberg, Lundalogik

A combo box with check boxes.
VC6, C++Windows, NT4, MFC, VS6, VS, Dev

Posted: 24 Nov 1999
Updated: 24 Nov 1999
Views: 217,407
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
54 votes for this Article.
Popularity: 8.26 Rating: 4.77 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
6 votes, 22.2%
4
21 votes, 77.8%
5

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

About the Author

Magnus Egelberg, Lundalogik



Location: Sweden Sweden

Other popular Combo & List Boxes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 73 (Total in Forum: 73) (Refresh)FirstPrevNext
Subject  Author Date 
Generalwhere can i find a version written by c#memberlinzhuo23:10 25 Jun '08  
GeneralLicensememberlanreola7:12 26 Mar '08  
Generalhow to disable check box selection ?membermrgoos5:40 19 Aug '07  
QuestionTool tip for Combo contolmembersajithomas23:52 21 Jun '07  
GeneralUsing CheckComboBoxmemberjives3:52 30 Apr '07  
GeneralWhy not CB_GETCOMBOBOXINFO?membertworca9:38 13 Apr '07  
GeneralRe: Why not CB_GETCOMBOBOXINFO?memberMagnus Egelberg21:29 15 Apr '07  
GeneralRe: Why not CB_GETCOMBOBOXINFO?memberNotre10:46 5 Nov '07  
Generalcombocheckboxmemberdnldldrch14:21 6 Dec '06  
GeneralRe: combocheckboxmemberMagnus Egelberg23:53 6 Dec '06  
QuestionOn resizing the dropdown list does not show upmemberhoc_ngo14:15 15 May '06  
GeneralBugmemberMarek Konopka4:37 14 Mar '06  
GeneralRe: BugmemberMarek Konopka4:51 14 Mar '06  
Generalhow to change it to the dropdown stylememberminibao3:52 6 Feb '06  
GeneralRe: how to change it to the dropdown stylemember.dan.g.15:51 9 Mar '06  
GeneralMutiple selection in the drop-down list boxmemberlanreola12:05 31 Aug '05  
GeneralRe: Mutiple selection in the drop-down list boxmember.dan.g.15:58 9 Mar '06  
GeneralFlat CheckboxmemberRafal Glowinski9:03 19 Jul '04  
GeneralThis is just what I needmemberedger20:29 5 Feb '04  
Generalondropdown eventmemberGAOFENG00717:05 12 Nov '03  
GeneralWinCE?memberajh19:32 5 Sep '03  
GeneralRe: WinCE?memberJoão Paulo Figueira1:13 2 Oct '03  
GeneralRe: WinCE?memberajh6:11 2 Oct '03  
GeneralRe: WinCE?memberJoão Paulo Figueira11:21 7 Oct '03  
GeneralLicensememberstevev@fieldbusinc.com4:54 9 Apr '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Nov 1999
Editor: Smitha Vijayan
Copyright 1999 by Magnus Egelberg, Lundalogik
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project