Download demo project - 22 Kb
Download demo files - 6 Kb
Do you want:
- a multicolumn combobox?
- a title for each column?
- to control the text color in a disabled combobox control?
- to show/edit "description" in the edit control of combobox and get "id" instead?
- to sort items in combobox as normal?
If all your answers are yes, you can use my class CMultiColumnComboBox used in my project. It is a owner-draw combo box class, derived from class CComboBox, but it's very easy to use. As shown above, it's works exactly like a normal combo box. No matter how many columns a combo box has, you do not need to modify the class. You can set a column to be shown and get text from BoundColumn, even a combo box is "drop down" one or "simple" one. When you key in chars in the edit box of "drop down" or "simple" combo box, items are searched in ShowColumn, not BoundColumn, but the result is in BoundColumn. If you do not like gray color text in your combo box when it's disabled, you can change its color.
How to use this control
- Put an Ownerdraw ComboBox in your resource dialog, copy MultiColumnCpmboBox.cpp and MultiColumnComboBox.h to the directory of your workspace or project, and insert follow line at top of your CPP file.
#include MultiColumnComboBox.h
- Add new member variables dialog class.
DDX_Control(pDX, IDC_COMBO1, m_ComboBoxControl1);
DDX_CBString(pDX, IDC_COMBO1, m_ComboBoxString1);
- In message map function OnInitDialog, use FormaComboBox to set m_TotalColumn, m_BoundColumnn and m_ShowColumn, this function must be called before other function called.
m_ComboBoxControl2.FormatComboBox(2, 0, 1);
- Then, use SetColumnWidth to set m_ColumnWidth for each column, use SetColumnAlignStyle to set m_ColumnAlignStyle for each column. If default value DT_LEFT is used, skip it. Use SetColumnTitle to set title for each column. If you do not want title, skip it.
m_ComboBoxControl2.SetColumnWidth(150, 0);
m_ComboBoxControl2.SetColumnAlignStyle(DT_LEFT, DT_CENTER);
m_ComboBoxControl1.SetColumnTitle("ID", "DESCRIPTION");
- Use CMultiColumnComboBox::AddRow instead of CComboBox::AddString to add a row to the list box of a combo box.
m_ComboBoxControl2.AddRow(ColumnString1, ColumnString0);
m_ComboBoxControl4.AddRow(ColumnString0, ColumnString1,
ColumnString2, ColumnString3);
- As you see, now you can use it as a normal combo box.
Notes:
Function SetColumnWidth, SetColumnAlignStyle and AddRow can be called with a variable number of arguments. You can modify AddRow to works as Printf.
If you need more help, free feel to ask me.