Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CComboBox with disabled items

0.00/5 (No votes)
14 Apr 2000 1  
combobox with disabled items

Introduction

This class extends the usual combo box with the possibility of disabling some items. Those items are listed in the combo box but they can't be selected by the user.

The basic derivation has four parts:

  1. By making the combo box owner-draw, we can draw the disabled items in a distinct color.
  2. Overriding the WM_LBUTTONUP handler of the enclosed list box, we can actually disable clicking on the disabled items.
  3. Overriding CharToItem handler, we can disable picking the disabled items by keyboard.
  4. And finally, by reacting to reflected CBN_SELENDOK, we can assure that a disabled item was not, by bug in CExtendedComboBox implementation, selected.

The reader is urged to carefully study the code to understand the internals of the CExtendedComboBox class.

The use of WM_CTLCOLORLISTBOX message for subclassing comes from another article.

How to Use the Class

The CExtendedComboBox class is a drop-in replacement of CComboBox. You simply #include "ExtendedComboBox.h" and instead of CComboBox class, you use CExtendedComboBox class in your CDialog-derived class.

The behaviour of CExtendedComboBox is customizable by overriding:

virtual BOOL IsItemEnabled(UINT) const;

This function takes index of item as an argument (the function must explicitly check that the argument is not, in fact, out of bounds). It returns TRUE/FALSE. The default implementation uses least significant bit of item data.

This class should work with all combo box styles (CBS_SIMPLE, CBS_DROPDOWN and CBS_DROPLIST), although it has been thoroughly tested only with CBS_DROPDOWN. The combo box must be marked as CBS_OWNERDRAWFIXED (CBS_OWNERDRAWVARIABLE is possible but would require some changes to the code) and CBS_HASSTRINGS.

I am interested in hearing any comments.

History

  • 15th April, 2000: Initial version

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.

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