Click here to Skip to main content
15,879,474 members
Articles / Desktop Programming / MFC
Article

A Multi Column ComboBox

Rate me:
Please Sign up or sign in to vote.
3.52/5 (11 votes)
30 Nov 19992 min read 427.6K   9K   83   68
A multicolumn, customizable, editable combobox
  • Download demo project - 22 Kb
  • Download demo files - 6 Kb

    Image 1 Image 2

    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

    1. 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
      
    2. Add new member variables dialog class.
      DDX_Control(pDX, IDC_COMBO1, m_ComboBoxControl1);
      DDX_CBString(pDX, IDC_COMBO1, m_ComboBoxString1);
      
    3. 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);
      
    4. 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");
      
    5. 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);
      
    6. 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.

  • 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
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralDropdown error,help,help, Pin
    ice_beauty19-Jun-09 22:18
    ice_beauty19-Jun-09 22:18 
    GeneralOnGetText incorrect return value Pin
    BobMiller10-Mar-09 17:24
    BobMiller10-Mar-09 17:24 
    Generalvs 2005 Pin
    DexteR Jenkins21-Jun-08 4:24
    DexteR Jenkins21-Jun-08 4:24 
    Answercrashes and similar Pin
    gbjbaanb11-Dec-07 22:49
    gbjbaanb11-Dec-07 22:49 
    GeneralRe: crashes and similar Pin
    Sergey K. Jul20226-Jan-24 6:51
    Sergey K. Jul20226-Jan-24 6:51 
    QuestionQuestion (Virtual List for more than 32736 items) ? Pin
    ana_v12325-Nov-06 9:22
    ana_v12325-Nov-06 9:22 
    Questionhow create multiline combobox Pin
    KondriSergey7-Feb-05 1:33
    KondriSergey7-Feb-05 1:33 
    GeneralComboBox Pin
    Asha Rams1-Aug-04 22:09
    Asha Rams1-Aug-04 22:09 
    GeneralFix for Windows XP and 2000 Pin
    Anonymous14-Jul-04 8:11
    Anonymous14-Jul-04 8:11 
    GeneralRe: Fix for Windows XP and 2000 Pin
    Anonymous10-Mar-05 12:25
    Anonymous10-Mar-05 12:25 
    Generalhelp needed Pin
    spidey82011-Feb-04 18:40
    spidey82011-Feb-04 18:40 
    GeneralDII Pin
    Marquis.D.J6-Nov-03 19:08
    Marquis.D.J6-Nov-03 19:08 
    Generalnew version please ! Pin
    Wiktor125-Sep-03 8:52
    Wiktor125-Sep-03 8:52 
    GeneralWindows 2000 and XP fix information Pin
    John Fisher2-Sep-03 11:40
    John Fisher2-Sep-03 11:40 
    GeneralRe: Windows 2000 and XP fix information Pin
    ctchen25-Sep-03 0:00
    ctchen25-Sep-03 0:00 
    GeneralRe: Windows 2000 and XP fix information Pin
    John Fisher25-Sep-03 2:00
    John Fisher25-Sep-03 2:00 
    GeneralRe: Windows 2000 and XP fix information Pin
    bradsmithatuw10-Oct-03 2:43
    bradsmithatuw10-Oct-03 2:43 
    GeneralOnly 1 column Pin
    Krzyzak_72-Jun-03 1:36
    Krzyzak_72-Jun-03 1:36 
    Generala new version, please ! Pin
    Cynicannibal1-Apr-03 3:38
    Cynicannibal1-Apr-03 3:38 
    Generalmulticolumn comboBox Pin
    takhs1517-Mar-03 23:27
    takhs1517-Mar-03 23:27 
    GeneralMulti column combo box Pin
    m12215-Mar-03 22:35
    m12215-Mar-03 22:35 
    GeneralRe: Multi column combo box Pin
    KondriSergey16-Mar-03 21:22
    KondriSergey16-Mar-03 21:22 
    GeneralWARNING regarding XP Pin
    Earl Allen29-Nov-02 19:19
    Earl Allen29-Nov-02 19:19 
    GeneralFor Windows2000 with VC6 SP5 Pin
    23-Sep-02 16:57
    suss23-Sep-02 16:57 
    GeneralRe: For Windows2000 with VC6 SP5 Pin
    heng098728-Jul-03 10:18
    heng098728-Jul-03 10:18 

    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.