Click here to Skip to main content
13,140,231 members (46,235 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 3 Mar 2000

VBLikeCombo - A Visual Basic Style Combo Box

, 5 Apr 2000
Rate this:
Please Sign up or sign in to vote.
Creates a combo box similar to those in Visual Basic.
  • Download demo project - 22 Kb
  • Download source files - 5 Kb
  • Introduction

    CgzCombo extends the CComboBox class to give ComboBox functionality similar to a Visual Basic combo box with the following features:
    • Two "columns": 1 visible and 1 hidden "index column"
    • Autoselect: from list box items. Based nearly entirely from example given by Chris Maunder
    • Notification: Sends a CBN_CLOSEUP message to parent window when user makes a valid selection using the mouse *or* by typing and moving off control.
    • Validation: will not let a user leave the control without a valid selection this is easy to modify if not appropriate for your app.
    • Escape key: pressing escape within combo box resets it to value it had when it first got the focus.
    • Note: you can easily modify to have both columns hidden and display a third text string. (which is why the stringlist is done the way it is) It's also quite easy to add more "columns".

    The class name is CgzCombo.

    How to use this control

    (assuming you already have a combo box control in a project)

    1. Copy gzcombo.h and gzcombo.cpp to your projects directory
    2. In visual studio click on Project->Add to project -> Files and select the two files you just copied to your project directory.
    3. Add the following line to the header file containing the definition of your CComboBox variable:
      #include "gzcombo.h"
    4. In the same header file, change the type of your combo box variable from CComboBox to CgzCombo.
    5. Handle ON_CBN_CLOSEUP in your app if you want to act immediately upon valid selection by user, otherwise...dont.
    There are only 4 extra functions you call to interact with the control:

    //Add items to the list (initialization) 
    void AddRow(CString text, CString ID);
    //Retrieve the "hidden column" value of the current selection
    CString GetCurrentRowID();
    //Clear combo box and associated string list
    void Clear();
    //Remove the current combo box item and associate string list item
    void RemoveCurrentRow();

    I have commented these functions quite liberally so that you should have no problem extending or modifying them.

    This control grew out of the need to port applications written in Microsoft Access to Visual C++. Our Access apps made heavy use of combo boxes with a hidden ID field. Typically a user would select from a list of easy to read text but the app actually requires a index value for database lookup, etc.

    The autoselect code was borrowed from Chris Maunder's autoselect example available at another source code site. I made a couple of modifications: I needed the combo to alert the parent window when a valid selection is made not only via mouse but if a user types in a partial string, an autoselect match is made and the user tabs out of the control.

    I override the reflected ON_KILLFOCUS message within the control and send a ON_CLOSEUP message to the parent window if the selection is valid. I tried ON_SELENDOK but had many problems with the order of events. As well for some reason Microsoft docs state that it's preferrable to use ON_CLOSEUP.

    I also modified Chris' example to watch for the escape key and if pressed while in the edit portion of the combo box will revert the edit box text back to the value it was at when the control first received focus. I did this because Access combo boxes allow you to do that and our clients are used to having this happen. (You would not believe how strongly people can feel about little details like this once they are used to them! <grin>)

    The hidden column is a CStringList object. When you look at the code you will see that the visible text in the list box is stored in the list box as normal but also stored as a column in the string list with the ID being a second column in the string list. Why did I do it like this? Because sometimes I want to display a completely different value to the user but still store the original text and ID column. You can easily remove or add more "columns" if you like.

    I use this control in dialog and FormView based projects. Developed in Visual studio 6, I'm not certain if it will compile in previous versions or not but it does compile cleanly under warning level 4.

    Please email me at if you find a bug, have a suggestion or question. <p>


    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

    Member 96
    Canada Canada
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralMy vote of 1 Pin
    Howard Richards29-Jan-09 22:10
    memberHoward Richards29-Jan-09 22:10 
    Generalcombo box code Pin
    net1231-Mar-08 2:05
    membernet1231-Mar-08 2:05 
    GeneralRe: combo box code Pin
    John C1-Mar-08 6:27
    memberJohn C1-Mar-08 6:27 
    Generalproblem help please Pin
    feritu23-Nov-06 23:40
    memberferitu23-Nov-06 23:40 
    GeneralRe: problem help please Pin
    feritu24-Nov-06 3:18
    memberferitu24-Nov-06 3:18 
    GeneralCombo Box in SDI application Pin
    Aditya Rao5-Aug-05 2:05
    memberAditya Rao5-Aug-05 2:05 
    GeneralNope Pin
    Klaus Probst6-Feb-02 13:49
    memberKlaus Probst6-Feb-02 13:49 
    GeneralRe: Nope Pin
    J Cardinal6-Feb-02 14:03
    memberJ Cardinal6-Feb-02 14:03 
    GeneralSearching in the ComboBox by Index Pin
    Bernhard18-Nov-01 22:25
    memberBernhard18-Nov-01 22:25 
    GeneralRe: Searching in the ComboBox by Index Pin
    J Cardinal6-Feb-02 14:12
    memberJ Cardinal6-Feb-02 14:12 
    GeneralThank you for your reply.. Pin
    Bernhard10-Feb-02 20:17
    memberBernhard10-Feb-02 20:17 
    GeneralSetting the Current Item Pin
    Giles26-Sep-01 6:16
    memberGiles26-Sep-01 6:16 
    GeneralRe: Setting the Current Item Pin
    BugZ RevengE4-Feb-02 14:37
    memberBugZ RevengE4-Feb-02 14:37 
    GeneralRe: Setting the Current Item Pin
    J Cardinal6-Feb-02 14:10
    memberJ Cardinal6-Feb-02 14:10 
    GeneralMemory Leak!!! Pin
    Mike Wild5-Apr-00 23:43
    sussMike Wild5-Apr-00 23:43 
    GeneralRe: Memory Leak!!! Pin
    JCardinal6-Apr-00 5:07
    sussJCardinal6-Apr-00 5:07 

    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
    Web03 | 2.8.170915.1 | Last Updated 6 Apr 2000
    Article Copyright 2000 by Member 96
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid