Click here to Skip to main content
14,299,406 members

Tree ComboBox Control

Rate this:
4.89 (16 votes)
Please Sign up or sign in to vote.
4.89 (16 votes)
16 Oct 2014CPOL
Tree ComboBox Control



Recently, I needed a tree control inside a combobox, and during my search on Google I found something, but it was a little more complicated than I required. I started to draft one myself, and am sharing the result with you.

Using the Code

Everything is based on CTreeComboBox, which is derived from the standard CComboBox. Here I have a CComboTreeCtrl variable member and methods characteristic to a tree control. In CTreeComboBox::PreSubclassWindow, I create and initialize m_Tree and stretch on the combobox dropdown list.

void CTreeComboBox::PreSubclassWindow() 
    // TODO: Add your specialized code here and/or call the base class


    CRect rect(0, 0, 0, 0);
    m_Tree.CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);


    dwStyle = 0x03 & GetStyle();

At first click, or arrow key down, the tree window will open.

void CTreeComboBox::OnLButtonDown(UINT nFlags, CPoint point) 
    // TODO: Add your message handler code here and/or call default

    m_bTree = ! m_bTree;

//    CComboBox::OnLButtonDown(nFlags, point);


BOOL CTreeComboBox::PreTranslateMessage(MSG* pMsg) 
    // TODO: Add your specialized code here and/or call the base class

    if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
        return TRUE;

    return CComboBox::PreTranslateMessage(pMsg);

But the tree window should close at second click, escape key, Enter key, or lost focus. I tried a few ways but I failed, so my solution was to send to the parent (CTreeComboBox) a message to close.

LRESULT CTreeComboBox::OnCloseControl(WPARAM wParam, LPARAM lParam)
    m_bTree = FALSE;

    return 1;

ComboBox must setup with CBS_DROPDOWNLIST or else you'll be ASSERTed :)

In order to use this control, you need #include "TreeComboBox.h" and further handle combobox like it is a tree control (you can see how in the sample project).

Enjoy it!


  • 26 April, 2011: Initial version.
  • 20 February, 2012: Added CTreeCtrl and GetTreeCtrl() methods to the CTreeComboBox class, so now it has full access to the tree control.
  • 10 May, 2013: Updated the control archive.
  • 16 Oct., 2014: The drop down is dropped by screen position (down or up); The drop down can not be closed by Alt+F4.
  • 14 Nov. 2014: I updated the code in order to be used on newer versions of Visual Studio (VS2008 for instance).
  • 18 Nov. 2014: I added posibility that combobox could be dropped down through F4. I added CComboTreeCtrlExt class in order to be customized tree control of combobox without mess up CComboTreeCtrl class.


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


About the Author

Romania Romania
No Biography provided

Comments and Discussions

QuestionI vote you 5 Pin
Matrix is everywhere20-Oct-14 15:23
memberMatrix is everywhere20-Oct-14 15:23 
AnswerRe: I vote you 5 Pin
_Flaviu20-Oct-14 20:32
member_Flaviu20-Oct-14 20:32 
GeneralMy vote of 2 Pin
stevehoang16-Oct-14 10:44
memberstevehoang16-Oct-14 10:44 
GeneralRe: My vote of 2 Pin
_Flaviu16-Oct-14 20:01
member_Flaviu16-Oct-14 20:01 
GeneralRe: My vote of 2 Pin
_Flaviu13-Nov-14 22:54
member_Flaviu13-Nov-14 22:54 
GeneralRe: My vote of 2 Pin
stevehoang26-Nov-14 8:48
memberstevehoang26-Nov-14 8:48 
GeneralRe: My vote of 2 Pin
_Flaviu26-Nov-14 21:26
member_Flaviu26-Nov-14 21:26 
QuestionProblems with this control Pin
stevehoang16-Oct-14 8:19
memberstevehoang16-Oct-14 8:19 
QuestionNice tree ComboBox Control! Pin
Volynsky Alex16-Oct-14 3:55
professionalVolynsky Alex16-Oct-14 3:55 
AnswerRe: Nice tree ComboBox Control! Pin
_Flaviu16-Oct-14 20:02
member_Flaviu16-Oct-14 20:02 
QuestionRe: Nice tree ComboBox Control! Pin
Volynsky Alex17-Oct-14 4:36
professionalVolynsky Alex17-Oct-14 4:36 
Questioni vote of 5 Pin
xunonxyz22-Mar-13 5:28
memberxunonxyz22-Mar-13 5:28 
QuestionWhen Click on the checkbox it can not hide and show noting on combox's Edit Pin
maplewang8-Aug-12 5:53
membermaplewang8-Aug-12 5:53 
GeneralMy vote of 5 Pin
huwuling3-May-12 5:10
memberhuwuling3-May-12 5:10 
GeneralRe: My vote of 5 Pin
_Flaviu11-May-12 2:11
member_Flaviu11-May-12 2:11 
Suggestionmodified in vs2003 Pin
magicpapacy11-Mar-12 16:30
membermagicpapacy11-Mar-12 16:30 
GeneralVS2008 : ComboTreeCtrl behind Pin
cdcl16-May-11 5:00
membercdcl16-May-11 5:00 
GeneralRe: VS2008 : ComboTreeCtrl behind [modified] Pin
_Flaviu18-May-11 19:38
member_Flaviu18-May-11 19:38 

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.

Posted 27 Apr 2011


67 bookmarked