Click here to Skip to main content
11,410,118 members (63,138 online)
Click here to Skip to main content

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 john@zerocentral.com if you find a bug, have a suggestion or question.

    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

    Share

    About the Author

    Member 96

    Canada Canada
    No Biography provided

    Comments and Discussions

     
    GeneralMy vote of 1 PinmemberHoward Richards29-Jan-09 23:10 
    Generalcombo box code Pinmembernet1231-Mar-08 3:05 
    GeneralRe: combo box code PinmemberJohn C1-Mar-08 7:27 
    Generalproblem help please Pinmemberferitu24-Nov-06 0:40 
    GeneralRe: problem help please Pinmemberferitu24-Nov-06 4:18 
    GeneralCombo Box in SDI application PinmemberAditya Rao5-Aug-05 3:05 
    GeneralNope PinmemberKlaus Probst6-Feb-02 14:49 
    GeneralRe: Nope PinmemberJ Cardinal6-Feb-02 15:03 
    GeneralSearching in the ComboBox by Index PinmemberBernhard18-Nov-01 23:25 
    GeneralRe: Searching in the ComboBox by Index PinmemberJ Cardinal6-Feb-02 15:12 
    GeneralThank you for your reply.. PinmemberBernhard10-Feb-02 21:17 
    GeneralSetting the Current Item PinmemberGiles26-Sep-01 7:16 
    GeneralRe: Setting the Current Item PinmemberBugZ RevengE4-Feb-02 15:37 
    GeneralRe: Setting the Current Item PinmemberJ Cardinal6-Feb-02 15:10 
    GeneralMemory Leak!!! PinsussMike Wild6-Apr-00 0:43 
    GeneralRe: Memory Leak!!! PinsussJCardinal6-Apr-00 6:07 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.150414.5 | Last Updated 6 Apr 2000
    Article Copyright 2000 by Member 96
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid