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

Tree ComboBox Control

By , 9 May 2013
 
TreeCComboBoxControl/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 :)

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
  • 10th May, 2013: Updated the control archive.

License

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

About the Author

Flaviu2
Romania Romania
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questioni vote of 5memberxunonxyz22 Mar '13 - 5:28 
it is great! and i modify this code /// rc.bottom-1////
/////
void CTreeComboBox::ShowDropDown()
{
 
if (m_pActiveCombo!=NULL)
{
HideDropDown();
//m_edit.SelectText(m_multiline);
m_edit.SetFocus();
return;
}
 

 
CRect rc;
GetWindowRect(&rc);
m_tree.SetWindowPos(&wndTopMost,rc.left, rc.bottom-1, rc.Width(), 150,SWP_SHOWWINDOW);
 
CWnd* pTopParent = GetParent()->GetParentOwner();
if (pTopParent != NULL)
{
pTopParent->SendMessage( WM_NCACTIVATE, TRUE );
pTopParent->SetRedraw( TRUE );
}
 
m_pActiveCombo = this;
}
QuestionWhen Click on the checkbox it can not hide and show noting on combox's Editmembermaplewang8 Aug '12 - 5:53 
So, it is a pity can not show changes on selection.
I am using VS2010.
GeneralMy vote of 5memberhuwuling3 May '12 - 5:10 
Thanks
GeneralRe: My vote of 5memberFlaviu211 May '12 - 2:11 
Thank you, I feel that I have to give back something to codeproject, because here are some many people that help me ...
Suggestionmodified in vs2003membermagicpapacy11 Mar '12 - 16:30 
in CTreeComboBox::PreSubclassWindow()
//m_Tree.CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
CWnd *pWnd = &m_Tree;
pWnd->CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
GeneralVS2008 : ComboTreeCtrl behindmemberMember 768188116 May '11 - 5:00 
Hello,
 
First of all CreateEx with WS_POPUP will ASSERT on CWnd::Create
     // can't use for desktop or pop-up windows (use CreateEx instead)
     ASSERT(pParentWnd != NULL);
     ASSERT((dwStyle & WS_POPUP) == 0);
 
I changed the code as follow :
     DWORD dwStyle =   WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | TVS_CHECKBOXES;
     m_Tree.Create(dwStyle, rect, GetParent(), NULL);
     m_Tree.ModifyStyle(0,WS_POPUP);
     m_Tree.SetParent(NULL);
 
Now it seems to work... But the ComboTreeBox appars behind the Dialog.
Any hint to resolve this is welcome!
 
cdcl
GeneralRe: VS2008 : ComboTreeCtrl behind [modified]memberFlaviu218 May '11 - 19:38 
Unfortunately , I didn't work in VS2008 , ( I never use this environment ) , so I try your solution in VC6 but didn't working ... I will study the issue and I hope to come back with a solution . Thanks for feedback .

modified on Thursday, May 19, 2011 5:11 AM

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 10 May 2013
Article Copyright 2011 by Flaviu2
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid