Click here to Skip to main content
15,914,010 members
Articles / Desktop Programming / MFC

CComboBox with disabled items

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
14 Apr 2000CPOL 230.7K   2.3K   41   33
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:

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Does not work for CBS_DROPDOWNLIST Pin
Anna-Jayne Metcalfe29-Jul-02 22:27
Anna-Jayne Metcalfe29-Jul-02 22:27 
GeneralRe: Does not work for CBS_DROPDOWNLIST Pin
Sasa Kajic8-Aug-02 4:45
Sasa Kajic8-Aug-02 4:45 
GeneralRe: Does not work for CBS_DROPDOWNLIST Pin
Ben Kraan3-Sep-03 4:51
Ben Kraan3-Sep-03 4:51 
QuestionRe: Does not work for CBS_DROPDOWNLIST Pin
Sharon F22-Apr-10 6:06
Sharon F22-Apr-10 6:06 
AnswerRe: Does not work for CBS_DROPDOWNLIST Pin
Anna-Jayne Metcalfe22-Apr-10 6:29
Anna-Jayne Metcalfe22-Apr-10 6:29 
GeneralRe: Does not work for CBS_DROPDOWNLIST [modified] Pin
SwimO27-Sep-18 16:42
SwimO27-Sep-18 16:42 
GeneralRe: Does not work for CBS_DROPDOWNLIST Pin
loxady28-Apr-08 13:14
loxady28-Apr-08 13:14 

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

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