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


93 bookmarked
Posted 27 Jul 2000


, 11 May 2003
Rate this:
Please Sign up or sign in to vote.
ComboBox with a tree control drop down
<!-- Article Starts -->

Sample Image

ComboTree is a hybrid ComboBox with a tree control replacing the drop down ListBox. Implementing a hierarchy of choices using multiple dependant combo or list boxes can take up a lot of screen space and can be awkward to use. A dropdown tree control can be a good replacement. As an option, the ComboTree supports a tree with three-state checkboxes for convenient multiple selection from a hierarchy of choices.

It has been tested and works on Win2000, Win98 and NT 4.0. Due to differences between these systems the technique of positioning a substitute control in place of the ListBox of an existing ComboBox upon receiving a CBN_DROPDOWN message couldn't be relied on. Instead of subclassing and modifying the behavior, this control totally replaces the existing ComboBox.

The previous version of ComboTree used a tree control with the WS_POPUP style that was a child of the ComboTree's parent window. The WS_POPUP style however caused the parent dialog to become inactive when the tree was dropped down. This version of ComboTree creates the tree control as a child of the Desktop window like the ComboBox does with its ListBox. The ComboTree subclasses the ComboTree's parent window to intercept messages when the tree is dropped down and it also forwards keyboard messages to the tree. The built in tree control's tooltips did not work in this configuration, so the ComboTree  includes a custom tooltip control to provide them.

Here are the steps needed to use a ComboTree in your project:

  1. Just as you normally would, place a ComboBox on the dialog. Set the dropdown height. The hybrid control will use the same sizes and positions as the original.
  2. Add all files named ComboTree*.cpp & .h and Subclass.cpp, Subclass.h, PupText.cpp, PupText.h to your project.
  3. If you will be using the checked tree style, import the bitmap file ComboTreeCheckboxes.bmp and give it a resource Id of "COMBOTREECHECKS" (Note: The quotes are important! The quoted resource form is used to allow the bitmap to be optional.) Alternatively, you may open the resource file for the sample project, ComboTreeShow.rc and drag and drop the "COMBOTREECHECKS" bitmap into your project's resource tab.
  4. In the header file for the dialog, add an include for ComboTree.h at the top. Manually add a ComboTree member variable, referred to as m_ComboTree below
  5. Create the ComboTree in the dialog's OnInitDialog() function. If you are using checkboxes you must call SetHasCheckboxes(TRUE) before creating the control. Create the ComboTree by subclassing the ComboBox from step one. Example syntax for creating a checked tree:
    m_ComboTree.SetHasCheckboxes(TRUE);<BR>m_ComboTree.SubclassDlgItem (IDC_COMBO_TREE);
    (To create a tree without checkboxes, omit the SetHasCheckboxes() call.)
  6. Populate the control using the AddString() function.
  7. Just as you normally would, use Class Wizard or edit manually to add handlers for the ComboBox notifications you want. ComboTree uses the same CBN_* notifications as a normal ComboBox.

Usage Notes:

Important note: Don't use ClassWizard and the DDX/DDV mechanism to add and associate member variables with the ComboBox. Once the original ComboBox control is subclassed it won't exist anymore.

Not all ComboBox notifications are supported, but the following ones are:
When accessing the control, call the ComboTree functions directly instead of sending messges. The following access functions which emulate their CComboBox equivalents are supported.
void SetWindowText (LPCTSTR Text);
CString GetWindowText ();
int GetLBText (HTREEITEM hItem, CString& rText);

HTREEITEM AddString ( LPCTSTR lpszString);
HTREEITEM FindString ( LPCTSTR lpszString, HTREEITEM hParent = NULL);
HTREEITEM SelectString( LPCTSTR lpszString, HTREEITEM hParent = NULL);

int   SetItemData (HTREEITEM hItem, DWORD dwItemData);
DWORD GetItemData (HTREEITEM hItem);
void ShowDropDown( BOOL bShowIt = TRUE );
void ResetContent ();
int  SetDroppedWidth( UINT nWidth );
int  GetDroppedWidth( );
BOOL GetDroppedState( );
int  DeleteString( HTREEITEM hItem );
void GetDroppedControlRect (LPRECT pRect);

The access functions differ in a couple of important ways from normal CComboBox functions. Functions that accept a string use the full path of the tree heirachy. For example to select the "Chipmunk" node use the full path to it:

hNode = m_ComboTree.SelectString ("Animals/Mammals/Chipmunk");

To populate the CComboBox use the AddString () function. The function will add nodes for all levels of the path that don't already exist. The HTREEITEM returned is for the end node. Example:

HTREEITEM hNode  = m_ComboTree.AddString ("Plants/Trees/Oak");

ComboTree functions accept or return a HTREEITEM where the CComboBox equivalent uses a ListBox index. CB_ERR is still used as an error return value in functions where the CComboBox equivalent uses 0 as a success return value.

In addition to the CComboBox emulation functions are the following extensions:.

// NOTIFY_TREECOMBO_CHECK notification message for check event. 
// Message map handler example: 

CString GetTreePath (HTREEITEM hItem);
CString GetCurrentTreePath ();
TCHAR GetPathDelimiter ();
void SetPathDelimiter (TCHAR Delimiter);
int  SetDroppedHeight (UINT nHeight);
int  GetDroppedHeight ();
HTREEITEM FindChildItemData(DWORD SearchData, HTREEITEM hItem = NULL) ;
HTREEITEM GetLastSibling( HTREEITEM hItem ) ;
void CollapseBranch( HTREEITEM hItem) ;
void ExpandBranch( HTREEITEM hItem ) ;
void CollapseAllSiblings( HTREEITEM hNode );
BOOL SetHasCheckboxes (BOOL bHasCheckboxes);
BOOL GetHasCheckboxes ();
BOOL SetCheck( HTREEITEM hItem, BOOL bChecked = TRUE );
BOOL IsItemChecked(HTREEITEM hItem);
HTREEITEM GetFirstCheckedItem();
HTREEITEM GetNextCheckedItem( HTREEITEM hItem );
HTREEITEM GetPrevCheckedItem( HTREEITEM hItem );

GetTreePath () and GetCurrentTreePath() return the text of the tree item prefixed with the concatenated text of its parent nodes.

Functions that contain "Check" in the name only are valid if the tree has a checked style

Three functions allow direct access to ComboTree child controls.

ComboTreeDropList& GetTree () ;
ComboTreeEdit& GetEdit () ;
ComboTreeButton& GetDropDownButton ();

GetTree(), GetEdit () and GetDropDownButton() allow direct access to the tree, edit and button controls. (Note: Use this direct access only in ways that do not conflict with the CBN_* notifications and maintenance of control state.)

The ComboTree doesn't have support for keyboard input to select tree items from within the edit control at this time. Text lookup similar to that of a drop down style combo box could be added with some additional search functionality. The edit control is currently read only.


  • 8 Aug 2000 - Fixed painting bug reported by Steve Roach as well as a few other things I overlooked in emulating the droplist style ComboBox, like highlighting the control when it it gets focus, dropping down the tree when the edit gets a mouse click and disabling mouse and context menu text manipulation.
  • 9 May 2003 - New version of ComboTree fixes all the known bugs, adds three-state checkbox. Thanks to all who reported them! Credits to Magerusan Grigore Cosmin's article for fix to dialog inactivation when tree is dropped down, Paul DiLascia's article for custom tooltips and Zafir Anjum article for three-state checkboxes.


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

Dennis Howard
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralDoesn't compile in .NET Pin
Bogustus17-Sep-03 6:36
memberBogustus17-Sep-03 6:36 
GeneralRe: Doesn't compile in .NET Pin
jbird219-Dec-03 17:21
memberjbird219-Dec-03 17:21 
GeneralRe: Doesn't compile in .NET Pin
liuzhiyong21-Jul-08 0:45
memberliuzhiyong21-Jul-08 0:45 
GeneralTree control is not hide. Pin
Roongrit1-Jul-03 17:17
memberRoongrit1-Jul-03 17:17 
GeneralRe: Tree control is not hide. Pin
Andreas Glaubitz7-Dec-04 23:31
memberAndreas Glaubitz7-Dec-04 23:31 
GeneralRe: Tree control is not hide. Pin
Anonymous23-May-05 9:10
sussAnonymous23-May-05 9:10 
GeneralBug in Release build Pin
Roongrit30-Jun-03 19:33
memberRoongrit30-Jun-03 19:33 
GeneralRe: Bug in Release build Pin
shuyangzjg9-Mar-14 1:54
membershuyangzjg9-Mar-14 1:54 
Generaldropdown button doesn't use current system colors Pin
nomadik6-Jan-03 5:03
membernomadik6-Jan-03 5:03 
GeneralRe: dropdown button doesn't use current system colors Pin
Yogurt17-May-04 3:10
memberYogurt17-May-04 3:10 
GeneralDisabling a CTreeCtrl Item Pin
msubbareddy7-May-02 2:05
membermsubbareddy7-May-02 2:05 
Generalnot perfect enough! Pin
G.Richard9-Jan-02 17:23
memberG.Richard9-Jan-02 17:23 
GeneralCombo Tree Pin
Tanvir13-Oct-01 3:36
memberTanvir13-Oct-01 3:36 
GeneralExComboBox Pin
Anonymous12-Oct-01 9:29
memberAnonymous12-Oct-01 9:29 
GeneralRe: ExComboBox Pin
rpr14-May-02 9:01
memberrpr14-May-02 9:01 
Generalpaint Pin
mack6-Sep-01 16:59
membermack6-Sep-01 16:59 
GeneralLooks great...... Pin
Cory11-Jan-01 12:47
memberCory11-Jan-01 12:47 
Generalthanx Pin
Val Samko14-Aug-00 13:47
sussVal Samko14-Aug-00 13:47 
QuestionEver hear about ComboBoxEx? Pin
Daniel Kopitchinski11-Aug-00 21:52
sussDaniel Kopitchinski11-Aug-00 21:52 
GeneralGreat Idea ! Pin
Mark Wickman10-Aug-00 18:19
sussMark Wickman10-Aug-00 18:19 
QuestionGreat, but why MFC? Pin
Alex Turc9-Aug-00 22:10
sussAlex Turc9-Aug-00 22:10 

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 12 May 2003
Article Copyright 2000 by Dennis Howard
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid