Read/Only CCombobox class

Have you ever wanted the CCombobox class to display
"readable" text when it is disabled? No matter whether
it is a drop-down or a drop-list type of combobox!

Here’s a class that’ll do that (and much more, although the
rest is not visually related)!

For example, in the pictures below, you can see the
"before" and "after" effects of disabling the
comboboxes using the standard "EnableWindow()" function
call to either enable/disable the combobox.

Before

After

The TComboBox class also provides mechanisms for quite a few
time saving headaches when working with comboboxes, such as:

  1. Function calls to add entries into the combobox with
    "hidden" string information (i.e. in the
    screens above, the data "Item Data 1,2" is
    associated with the first combobox but is not visable). I
    know that Microsoft provides the
    "SetItemData()" member function for comboboxes,
    but it only allows for a DWORD, not a string. Therefore,
    there is no need to have global variables defined to
    contain the "hidden" information. Such an
    example is trying to use a CStringArray as data to be
    associated with the various entries in the combobox. As
    soon as the CStringArray goes out of scope, specifically
    if it is defined locally to a function, the data for the
    combobox will no longer be valid once the function exists.
  2. The ability to select an item based on the
    "hidden" code (i.e. in the above example, the
    statement to select the item in the third combobox was
    basically ‘m_cbcDropDownNew.SelectCode(-1, "Item
    Data 1,2")’.
  3. The ability to "force fill" the combobox if the
    information isn’t found. It is displayed differently
    (i.e. with special characters before/after the entry), to
    let you know that the entry was forced (the characters
    displayed are by default ‘+’ and ‘;’ – so a forced item
    would look like "+Drop List 3;").
  4. Let the combobox deal with duplicates (whether you want
    duplicates, or you don’t).
  5. Function calls to switch the mode from drop-down to
    drop-list (so that you don’t have to remember the style
    code necessary to switch to either).

Once you get the code, it is simple enough to add to whatever
project you are working on, by opening up ClassWizard, and
clicking on the "Add Class…" button in the upper
right hand corner of ClassWizard. Click on "New…"
from the drop down menu. The class name should be entered as
CTComboBox, and the base class should be CCombobox. After
clicking on OK, the files "TComboBox.h" and
"TComboBox.cpp" will be generated. Once that is done,
simply replace the files with the two that come with the demo
project. I’d like to give you an easier way to do this, but I’m
using VC 5.0, and haven’t been able to find out any other way to
add a class to a project (P.S. if anyone knows a better way, feel
free to recommend one!).

Once you’ve added the class to the project, all you have to do
to add member variables that are based on the
"CTComboBox" class, is to "Add variable…",
give the variable a name, set the "Category" to
control, and a select "variable type" of
"CTComboBox". ClassWizard will of course remind you
that you need to include the "tcombobox.h" file in the
header of the dialog itself.

Downloads

Download demo project – 27 Kb
Download source – 8 Kb

More by Author

Must Read