<!-- Link to source file download -->
Download source files - 2 Kb
<!-- Add the rest of your HTML here -->
From time to time, I have needed to have a listbox with fixed contents but with some of the items disabled. I
have tried to locate such a class but never succeeded. Therefore I decided to create the class on my own.
The source files of the class are available for download at the top of this article. To use the class, you simply
-derived class. (The class was not tested with
has been tested with
construction, and quite surely it will work fine with
The class behaviour is customizable by overriding the virtual method:
virtual BOOL IsItemEnabled(UINT) const;
This method takes the item number as argument (the code should explicitely check whether the argument is
out-of-bounds) and returns TRUE/FALSE. The default implementation uses the least significant bit of item data for
this logic. (It's easy to see that LSB is available for pointer data, since the pointers to structures are
DWORD-aligned; for integer data, you can do
data=stored_data>>1 to store and restore the data for the element.) By overriding the method, you
can alter the behaviour (and even make it selection-dependent!).
The code works for all types of listbox selection.
The code makes no attempt to disable the selection itself. It only draws the disabled item as disabled. It's up
to the programmer to handle cases when disabled item is selected.
Comments are welcome.