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

The Ultimate Toolbox Multi Combobox

0.00/5 (No votes)
25 Aug 2007 1  
A flexible multi-column combobox control from the Ultimate Toolbox

Visit the Ultimate Toolbox main page for an overview and configuration guide to the Ultimate Toolbox library.

Source code and project files for this sample can be found in the samples\gui\MultiCombo directory of the sample projects download.

Overview

The COXMulticomboBox class is derived from MFC's CComboBox and implements a combo box with more than one column. A version of this type of control can be seen in action in Microsoft Access.

Features

  • Can have multiple columns in each item of the combo box.
  • Only one column will be transferred to the edit (or static) control. The other columns are shown in the dropdown listbox as extra information.
  • The interface is identical to CComboBox with an extra column number parameter.

Usage

The ComboBox should be created as CBS_OWNERDRAWFIXED. If you want variable height rows you can use CBS_OWNERDRAWVARIABLE, but you will need to handle the MeasureItem notification yourself. If you don't specify CBS_HASSTRINGS, you have to handle the DrawItem and CompareItem methods and maintain each column items data yourself.

If the COXMultiComboBox is used as a control in dialog box then set the above said (ownerdrawn) styles in the dialog templates.

In the header of your MFC CDialog derived class, you can subclass a CComboBox member variable set by the Class Wizard by simply replacing the class name in the declaration in the AFX_DATA area.

The code from the samples\gui\MultiCombo:

#include "OXMultiComboBox.h"


#include "UTB64Bit.h"

/////////////////////////////////////////////////////////////////////////////

// CMComboDlg dialog

class CMComboDlg : public CDialog
{
// Construction

public:
    CMComboDlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data

    //{{AFX_DATA(CMComboDlg)

    enum { IDD = IDD_MCOMBO_DIALOG };
    COXMultiComboBox m_MCombo;

All that's needed is a bit of initialization in OnInitDialog:

    m_MCombo.SetColumnCount(3);
    m_MCombo.SetColumnWidth(0,100);
    m_MCombo.SetColumnWidth(1,120);
    m_MCombo.SetColumnWidth(2,20);
    LPTSTR str1[] = {_T("David"), _T("Cunningham"),
        _T("1")};
    LPTSTR str2[] = {_T("Troy") , _T("Marchand"),  
        _T("2")};
    LPTSTR str3[] = {_T("Andrei"), _T("Zenkovitch"), 
        _T("3")};
    LPTSTR str4[] = {_T("Ghazi"), _T("Wadi"), 
        _T("4")};

The compiled HTML help documentation contains a complete COXMulticomboBox class reference.

History

Initial CodeProject release August 2007.

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