Click here to Skip to main content
13,256,123 members (49,276 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 3 Apr 2000

CListBox with disabled items

, 14 Apr 2000
Rate this:
Please Sign up or sign in to vote.
Listbox with disabled items.
<!-- 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.

    Class CExtendedListBox

    The source files of the class are available for download at the top of this article. To use the class, you simply #include "ExtendedListBox.h" and use CExtendedListBox instead of CListBox in your CDialog-derived class. (The class was not tested with Create() construction; it has been tested with DDX_Control() construction, and quite surely it will work fine with SubclassWindow().)

    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_to_store=(data<<1)|is_enabled and 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.


    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


    About the Author

    Petr Novotny
    Czech Republic Czech Republic
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralMy vote of 4 Pin
    namezero11111110-Sep-12 9:19
    membernamezero11111110-Sep-12 9:19 
    QuestionPrevent selection in ListControl Pin
    Ashwini Appajigowda25-Jul-07 18:29
    memberAshwini Appajigowda25-Jul-07 18:29 
    Generalerror in my project Pin
    steve sawyer16-Feb-02 16:29
    membersteve sawyer16-Feb-02 16:29 
    GeneralRe: error in my project--never mind-- Pin
    steve sawyer16-Feb-02 16:32
    membersteve sawyer16-Feb-02 16:32 
    GeneralAdditional Information Pin
    Neil12-Jul-00 2:39
    sussNeil12-Jul-00 2:39 
    GeneralRe: Additional Information Pin
    namezero11111110-Sep-12 9:19
    membernamezero11111110-Sep-12 9:19 

    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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.171114.1 | Last Updated 15 Apr 2000
    Article Copyright 2000 by Petr Novotny
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid