Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CTreeComboBox: A Multiline Tree Combo Box that supports XML

0.00/5 (No votes)
25 Aug 2004 1  
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