Click here to Skip to main content
13,096,858 members (85,411 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 31 May 2000


, 28 Jun 2000
Rate this:
Please Sign up or sign in to vote.
An extremely simple but useful CComboBox entension
<!-- Link to source file download -->
  • Download source files - 2 Kb
  • <!-- Link to demo file download -->
  • Download demo project - 12 Kb
  • If you use CComboBox (in Drop List mode), you know it's sometimes awkward to use in situations where you need the user to pick from a set of enumerated values. You need to prevent the list box strings from being sorted and keep track of which string is in which position, or worse, do string comparisions to find out which item is selected.

    Our traditional method was to load up the list box with the strings in a fixed order - one string per enumerated value, in the same order as the enumerated values. Then we would use CComboBox::GetCurSel() to get the currently selected item; this value would tell us which string was selected.

    For example

    typedef enum eEnumeratedFruit {eApple, eOrange, eCherry};
    const char *pStrings[] = {"Apple", "Orange", "Cherry"};
    for (int i=0; i < numStrings; i++)
    // find out which was selected
    eEnumeratedFruit eSelFruit = (eEnumeratedFruit)m_combo.GetCurSel();

    This works fine... until we want to re-order the strings without changing the order of the enumerated values. For example, if we wanted to put the fruits into the list box in alphabetical order (Apple, Cherry, Orange) this code would fail because the enum is ordered Apple, Orange, Cherry. Even worse, if we only wanted to skip items in the middle of the enumeration (ex. Apple, Cherry, not Orange)... Using CComboBox, there is simply no way to handle that situation in an elegant and generic fashion - without doing exactly what CItemDataCombo does, of course.

    So, we wrote the CItemDataCombo box.

    CItemDataCombo is a very simple class. There are only three functions:

    // a specialized AddString 
    int   AddString(const char *pString, DWORD dwData);
    // get the item data for the currently selected item
    DWORD GetCurSelData();
    // select the string that has dwData for its item data
    int   SelectItemData(DWORD dwData);
    For example:
    typedef enum eEnumeratedFruit {eBanana, 
    // add the strings, along with their associated enum value
    m_combo.AddString("Banana", (DWORD)eBanana);
    m_combo.AddString("Apple", (DWORD)eApple);
    m_combo.AddString("Orange", (DWORD)eOrange);
    m_combo.AddString("Grape", (DWORD)eGrape);
    m_combo.AddString("Blueberry", (DWORD)eBlueberry);
    m_combo.AddString("Cherry", (DWORD)eCherry);
    // select the item that corresponds to the eApple value
    // which value has the user selected?
    eEnumeratedFruit eSelFruit = (eEnumeratedFruit)m_combo.GetCurSelData();	

    Using CItemDataCombo, you can add the strings in any order, use any subset of possible enumerated values in the box, etc..

    It has simplified CComboBox usage tremendously. No more worrying about the order of the list box strings! Plus, we can leave the "Sort" property on in the dialog editor, which sometimes makes things nice for users.

    The only annoying part of the whole thing is the need to cast to and from the enumerated value when setting, getting data. But, this could be easily solved with a templated class.


    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


    About the Author

    Chris Losinger
    Software Developer
    United States United States
    Chris Losinger is the president of Smaller Animals Software, Inc..

    You may also be interested in...

    Comments and Discussions

    QuestionThanks - useful and instructive Pin
    lctrncs31-May-06 12:23
    memberlctrncs31-May-06 12:23 
    Questionhow to link combo box to dialog Pin
    jinshi22-Jan-05 20:00
    memberjinshi22-Jan-05 20:00 
    GeneralThanks you very much! Pin
    alexdhu17-May-03 17:22
    memberalexdhu17-May-03 17:22 
    GeneralThanks Pin
    P Young20-Jul-01 2:01
    memberP Young20-Jul-01 2:01 
    GeneralSource File Link Broken Pin
    VR29-Jun-00 12:28
    sussVR29-Jun-00 12:28 
    GeneralGive a snapshot! Pin
    M D 29-Jun-00 0:03
    suss M D 29-Jun-00 0:03 
    GeneralRe: Give a snapshot! Pin
    chris losinger29-Jun-00 9:13
    susschris losinger29-Jun-00 9:13 

    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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.170813.1 | Last Updated 29 Jun 2000
    Article Copyright 2000 by Chris Losinger
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid