Download demo project - 22 Kb
Download source files - 5 Kb
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
How to use this control
(assuming you already have a combo box control in a project)
- Copy gzcombo.h and gzcombo.cpp to your projects directory
- In visual studio click on Project->Add to project -> Files and select
the two files you just copied to your project directory.
- Add the following line to the header file containing the definition of your
- In the same header file, change the type of your combo box variable from
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:
void AddRow(CString text, CString ID);
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
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 email@example.com if you find
a bug, have a suggestion or question.