Click here to Skip to main content
Click here to Skip to main content

Tree ComboBox Control

, 20 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Tree ComboBox Control
This is an old version of the currently published article.
treecombobox.gif

Introduction

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 standard CComboBox. Here I have CComboTreeCtrl variable member and methods characteristic to tree control. In CTreeComboBox::PreSubclassWindow, I create and initialize m_Tree and stretch on combobox dropdown list.

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

    CComboBox::PreSubclassWindow();

    CRect rect(0, 0, 0, 0);
    DWORD dwStyle =  WS_POPUP | WS_BORDER | TVS_DISABLEDRAGDROP | 
    TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | 
    TVS_CHECKBOXES;
    m_Tree.CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
    m_Tree.Init(this);

    GetClientRect(rect);
    SetDroppedWidth(rect.Width());
    SetDroppedHeight(m_droppedHeight);

    dwStyle = 0x03 & GetStyle();
    ASSERT(dwStyle == CBS_DROPDOWNLIST);
}

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;
    if(m_bTree)DisplayTree();

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

and:

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)
    {
        DisplayTree();
        return TRUE;
    }

    return CComboBox::PreTranslateMessage(pMsg);
}

But 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) message to close.

LRESULT CTreeComboBox::OnCloseControl(WPARAM wParam, LPARAM lParam)
{
    TreeCtrlDone();
    m_Tree.ShowWindow(SW_HIDE);
    m_bTree = FALSE;

    return 1;
}

ComboBox must setup with CBS_DROPDOWNLIST or else you'll be ASSERT Smile | :)

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

Enjoy it!

History

  • 26th April, 2011: Initial version
  • 20th February, 2012: Added CTreeCtrl & GetTreeCtrl() method to CTreeComboBox class, so now it has full access to tree control

License

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

Share

About the Author

Flaviu2

Romania Romania
No Biography provided

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionI vote you 5 PinmemberMatrix is everywhere20-Oct-14 16:23 
AnswerRe: I vote you 5 PinmemberFlaviu220-Oct-14 21:32 
GeneralMy vote of 2 Pinmemberstevehoang16-Oct-14 11:44 
GeneralRe: My vote of 2 PinmemberFlaviu216-Oct-14 21:01 
GeneralRe: My vote of 2 PinmemberFlaviu213-Nov-14 23:54 
GeneralRe: My vote of 2 Pinmemberstevehoang26-Nov-14 9:48 
GeneralRe: My vote of 2 PinmemberFlaviu226-Nov-14 22:26 
QuestionProblems with this control [modified] Pinmemberstevehoang16-Oct-14 9:19 
QuestionNice tree ComboBox Control! PinprofessionalVolynsky Alex16-Oct-14 4:55 
AnswerRe: Nice tree ComboBox Control! PinmemberFlaviu216-Oct-14 21:02 
QuestionRe: Nice tree ComboBox Control! PinprofessionalVolynsky Alex17-Oct-14 5:36 
Questioni vote of 5 Pinmemberxunonxyz22-Mar-13 6:28 
QuestionWhen Click on the checkbox it can not hide and show noting on combox's Edit Pinmembermaplewang8-Aug-12 6:53 
GeneralMy vote of 5 Pinmemberhuwuling3-May-12 6:10 
GeneralRe: My vote of 5 PinmemberFlaviu211-May-12 3:11 
Suggestionmodified in vs2003 Pinmembermagicpapacy11-Mar-12 17:30 
GeneralVS2008 : ComboTreeCtrl behind PinmemberMember 768188116-May-11 6:00 
GeneralRe: VS2008 : ComboTreeCtrl behind [modified] PinmemberFlaviu218-May-11 20:38 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 20 Feb 2012
Article Copyright 2011 by Flaviu2
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid