Click here to Skip to main content
15,887,683 members
Articles / Desktop Programming / MFC

Category Listbox

Rate me:
Please Sign up or sign in to vote.
4.93/5 (9 votes)
24 Jun 2002CPOL2 min read 112.8K   3.6K   60   8
Control to mimic behavior of Outlook's categorized listbox
In this post, you will see a control that mimics the behaviour of Microsoft Outlook's categorized listbox
Image 1 Image 2
Without checkboxes (Default)
With checkboxes

Description

This control mimics the behavior of Microsoft Outlook's categorized listbox. It displays a list of categories and each category has its own list of items. Categories can be opened to reveal their items or closed to hide them. The idea is to help improve list organization and to make it easier for users to find what they are looking for.

Categories have the following attributes:

  • Are indicated in the list with a grey background.
  • Category name must be unique. (They are case sensitive.)
  • Can have 0 to N items under them.
  • Have open/close buttons to show/hide their items.
  • Can be opened/closed by double clicking them or by pressing the space bar.

Category items have the following attributes:

  • Must be assigned to a category
  • Item name does not have to be unique
  • Can have a checkbox displayed next to it (Microsoft Outlook does not have this feature.)
  • Checkboxes can be checked/unchecked by clicking them or by pressing the space bar.
  • Items can store DWORD data with them. (CListBox has this feature.)

Other supported features:

  • Sorts categories and their items if the LBS_SORT style has been set
  • Supports selection modes Single, Multiple, Extended, and None
  • SHOULD support unicode (I haven't verified this.)

Implementation

The category listbox class is derived from the MFC CListBox class. Most of CListBox’s functions can still be used, however, some functions have been protected thereby forcing you to use this class's functions instead. You cannot use the following CListBox functions with this class:

C++
AddString( LPCTSTR pString );
InsertString( int iIndex, LPCTSTR pString );
DeleteString( int iIndex );
GetItemData( int iIndex );
SetItemData( int iIndex, DWORD dwValue );

The category listbox class has been made as simple as possible to make it easy for you to add this control to your project. You only need to add the files "CatListBox.cpp" and "CatListBox.h" to your project. That's it! You do not have to add any images to your resource file because this class draws its buttons and checkboxes itself.

To add this control to your dialog, do the following:

  1. Add a listbox to your dialog's resource.
  2. Setup your listbox's resource for "Owner Draw: Fixed" and check "Has Strings".

    Image 3

  3. Create a CCatListBox member variable in your dialog's code. For example...
    C++
    #include "CatListBox.h"
    class MyDialog : public CDialog
    {
       public:
       // Dialog Data
       //{{AFX_DATA( MyDialog )
       enum { IDD = IDD_MY_DIALOG };
       CCatListBox   m_lstCategories;        // Create your variable here.
       //}}AFX_DATA
    }
    
    // Subclass the listbox here.
    // Make sure to replace IDC_LISTBOX_ID with the one you're using.
    void MyDialog::DoDataExchange( CDataExchange* pDX )
    {
       CDialog::DoDataExchange( pDX );
       //{{AFX_DATA_MAP( MyDialog )
       DDX_Control( pDX, IDC_LISTBOX_ID, m_lstCategories );   // Subclass it!
       //}}AFX_DATA_MAP
    }
    

History

  • 25th June, 2002: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLicence? Pin
peterchen14-Jan-09 22:53
peterchen14-Jan-09 22:53 
AnswerRe: Licence? Pin
Joshua Quick15-Jan-09 19:29
Joshua Quick15-Jan-09 19:29 
GeneralRe: Licence? Pin
peterchen15-Jan-09 20:43
peterchen15-Jan-09 20:43 
Thanks for the fast reply!

We'll be using it for selecting the result elements for a report generator. I've changed the category buttons to scrollup and scrolldown.

I want "free" to be "free". A commercial licence can be more restrictive, but free code I expect to be free for commercial, modifyable, perpetual, and noninfective. Preserving the copyright and licence statement is about the only thing I demand myself. Yes, that's an ego thing Smile | :)

"Free only for noncommercial use" makes it mostly useless to me. My mantra for this is: "Making money of my free code is work, too. Otherwise I would do it". Still, I understand if someone makes this choice.

"Publish modifications" - I don't think that should be a licence restriction. Asking for bug fixes and enhancements to be made available (to the author) should be enough. Usually, the guys writing good code are also happy to share it.

"Attribution" (in documentation or visible in the software) - now that's hard. I fully understand the joy that comes from it seeing your name in a classy product, but also I know that it makes the code basically a "no-go" in many companies, as contents of about screen and documentation is out of reach for the developers. So, another point for the authiors wishlist.

"classic open source" (GPL-flavor) is the worst for me: posing as free, it tries to put personal beliefs (hoiw software should be developed) into a contract.


GeneralRe: Licence? Pin
Joshua Quick15-Jan-09 21:19
Joshua Quick15-Jan-09 21:19 
GeneralExcellent piece of code! Pin
Karen030223-Aug-03 6:08
Karen030223-Aug-03 6:08 
GeneralHighlighting Pin
dudic27-Apr-03 9:00
dudic27-Apr-03 9:00 
GeneralRe: Highlighting Pin
Joshua Quick28-Apr-03 7:36
Joshua Quick28-Apr-03 7:36 
GeneralVery good - Could expand a bit more on how it works Pin
Paul Belikian26-Sep-02 12:01
Paul Belikian26-Sep-02 12:01 

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.