Click here to Skip to main content
15,891,633 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.1K   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

 
Generalremoving a combo box item during runtime Pin
Sasikumar.SR26-Jul-07 1:17
Sasikumar.SR26-Jul-07 1:17 
GeneralChoosing which items to disable Pin
twizted_sc29-Jun-06 23:33
twizted_sc29-Jun-06 23:33 
GeneralRe: Choosing which items to disable Pin
hengyulo10-Aug-06 1:12
hengyulo10-Aug-06 1:12 
GeneralRe: Choosing which items to disable Pin
uthamanmcadba29-Sep-09 1:25
uthamanmcadba29-Sep-09 1:25 
GeneralProject with ExtendedComboBox Class Pin
Karl Bahr13-Jan-04 0:48
Karl Bahr13-Jan-04 0:48 
GeneralRe: Project with ExtendedComboBox Class Pin
Pabs(Sco)24-Nov-05 2:55
Pabs(Sco)24-Nov-05 2:55 
Generaldisable item when selecting it Pin
bruno leclerc21-May-03 0:16
bruno leclerc21-May-03 0:16 
Hi,
my need is quit diferent,
When populating the list, i can't know if the items are enable or not (for performance).
I just want to forbid selection when the user try to do so.
Who can I do ?
GeneralRe: disable item when selecting it Pin
laxmanHCL8-Apr-05 23:49
laxmanHCL8-Apr-05 23:49 
GeneralRe: disable item when selecting it Pin
bruno leclerc11-Apr-05 2:57
bruno leclerc11-Apr-05 2:57 
GeneralRe: disable item when selecting it Pin
hengyulo10-Aug-06 1:32
hengyulo10-Aug-06 1:32 
Generalsimilar control for VB Pin
14-Mar-02 20:47
suss14-Mar-02 20:47 
GeneralCombo Box height Pin
16-Nov-01 7:23
suss16-Nov-01 7:23 
GeneralHiding some items in the Combobox Pin
29-Oct-01 4:11
suss29-Oct-01 4:11 
GeneralExPropertiesList - commercial troll Pin
Dean Wyant30-Aug-07 3:34
Dean Wyant30-Aug-07 3:34 
GeneralEnable disabled items Pin
Manon Perreault7-Aug-00 3:26
sussManon Perreault7-Aug-00 3:26 
GeneralRe: Enable disabled items Pin
Petr Novotny7-Aug-00 3:33
Petr Novotny7-Aug-00 3:33 
GeneralBox seams to be too high Pin
Dieter Fauth2-Aug-00 3:07
sussDieter Fauth2-Aug-00 3:07 
GeneralRe: Box seams to be too high (work around) Pin
Dieter Fauth29-Aug-00 0:24
sussDieter Fauth29-Aug-00 0:24 
GeneralRe: Box seams to be too high Pin
20-Apr-01 8:32
suss20-Apr-01 8:32 
GeneralBug fix - keys in non-dropped state Pin
Radim Krampol23-May-00 21:44
sussRadim Krampol23-May-00 21:44 
GeneralDoes not work for CBS_DROPDOWNLIST Pin
Andreas Gut17-Apr-00 23:27
Andreas Gut17-Apr-00 23:27 
GeneralRe: Does not work for CBS_DROPDOWNLIST Pin
Petr Novotny17-Apr-00 23:32
Petr Novotny17-Apr-00 23:32 
GeneralRe: Does not work for CBS_DROPDOWNLIST Pin
Dieter Fauth2-Aug-00 3:10
sussDieter Fauth2-Aug-00 3:10 
GeneralRe: Does not work for CBS_DROPDOWNLIST [modified] Pin
Anna-Jayne Metcalfe11-Sep-00 3:02
Anna-Jayne Metcalfe11-Sep-00 3:02 
GeneralRe: Does not work for CBS_DROPDOWNLIST Pin
Anonymous29-Jul-02 12:30
Anonymous29-Jul-02 12:30 

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.