![]() |
Desktop Development »
Combo & List Boxes »
ComboBox Controls
Intermediate
VBLikeCombo - A Visual Basic Style Combo BoxBy John CCreates a combo box similar to those in Visual Basic. |
VC6, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
CgzCombo extends the CComboBox class to give ComboBox functionality
similar to a Visual Basic combo box with the following features:
CBN_CLOSEUP message to parent window when user makes
a valid selection using the mouse *or* by typing and moving off control.
The class name is CgzCombo.
CComboBox variable:#include "gzcombo.h"
CComboBox to CgzCombo.
ON_CBN_CLOSEUP in your app if you want to act immediately
upon valid selection by user, otherwise...dont.
//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.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 5 Apr 2000 Editor: Valerie Bradley |
Copyright 2000 by John C Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |