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"
class CMComboDlg : public CDialog
{
public:
CMComboDlg(CWnd* pParent = NULL);
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.
Initial CodeProject release August 2007.