Click here to Skip to main content
15,558,581 members
Articles / Desktop Programming / MFC
Article
Posted 25 Aug 2004

Stats

88.2K views
3.4K downloads
46 bookmarked

CTreeComboBox: A Multiline Tree Combo Box that supports XML

Rate me:
Please Sign up or sign in to vote.
4.52/5 (9 votes)
25 Aug 20042 min read
This class implements a combo box that reads its data from an XML file, displays a tree as a drop down window, and supports mulitple selections.

Sample Image

Introduction

This articles presents the CTreeComboBox which should actually have been called CIconMultilineXmlTreeComboBox.

It's a CButton derived class with two major features:

  • XML file support: The control displays a tree control as a drop down window. The tree control loads its data from XML files!
  • Multiline edit box: The edit box of the combo box can have the 'multiline' style so that it can support the selection of many items from the tree!

Other features include:

  • sorting of items for the multiline version.
  • icon support for both the tree control and the singe-line version of the control.

Using the code

Put an owner draw button in a dialog (i.e., IDC_MYCOMBO).

Declare a member variable for the control.

CTreeComboBox m_mycombo;

Assign it to the button (in the DoDataExchange method of your dialog).

DDX_Control(pDX, IDC_MYCOMBO, m_mycombo);

Load an XML file:

m_mycombo.LoadXml("countries.xml");

or even better, load an XML file and a bitmap that contains the image list for the tree.

m_mycombo.LoadXml("countries.xml",IDB_TREE);

That's it! If you want to get the data you selected, declare a CString variable:

CString m_text;

and assign it to the control (in the DoDataExchange method of your dialog).

DDX_Text(pDX, IDC_MYCOMBO, m_text);

Remarks

If the height of the button is larger than 32, the combo box will become automatically multiline, otherwise it will be a normal single line combo box.

The first icon of the bitmap that behaves as an imagelist is used by the class as the folder icon. The second icon is for the expanded folders. The third is for the leaves of the tree. The icons of the leaves are also displayed in the edit box in the single line version of the control.

The multiline version of the control supports alphabetical sorting of the selected items. You can enable or disable this feature using the method EnableSort(BOOL).

Credits

The tree support was based on the tree-based combo boxes by Dennis Howard and Hai Ha.

The code for XML document handling has been written by Pablo van der Meer.

History

5 August 2004: first version.

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


Written By
Web Developer
Greece Greece
I've studied Electrical and Computer Engineer in
National Technical University of Athens.

I work as a software engineer since 1998.

Comments and Discussions

 
QuestionShould have Unicode support Pin
Mattias G3-Dec-12 23:56
Mattias G3-Dec-12 23:56 
QuestionAdded TVN_SELCHANGED msg Pin
Member 912173927-Jul-12 14:05
Member 912173927-Jul-12 14:05 
QuestionAdd AbsoluteChildrenSelectableOnly support Pin
Leslie Zhai20-Nov-11 20:20
Leslie Zhai20-Nov-11 20:20 
It works like children selection only ComboBoxTree, just added some code in the void CTreeComboBox::Accept() function shown as below:

C++
// About the line 197
HTREEITEM item = m_tree.GetSelectedItem();
if (m_tree.ItemHasChildren(item))
{
    MessageBox("NOT child node", "DEBUG", MB_OK);
    return;
}

QuestionHigh CPU load Pin
Christian Stelter27-Feb-07 11:21
Christian Stelter27-Feb-07 11:21 
AnswerRe: High CPU load Pin
John Melas5-Mar-07 0:29
John Melas5-Mar-07 0:29 
QuestionMore problems Pin
MarkBishop8-Aug-06 14:08
MarkBishop8-Aug-06 14:08 
AnswerRe: More problems Pin
John Melas31-Aug-06 0:48
John Melas31-Aug-06 0:48 
GeneralNot compiling under VC7 Pin
mile12346-Sep-04 23:17
mile12346-Sep-04 23:17 
GeneralRe: Not compiling under VC7 Pin
John Melas31-Aug-06 3:53
John Melas31-Aug-06 3:53 

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.