Category Listbox | CodeGuru

Category Listbox

Without checkboxes. (Default) With checkboxes. Environment: Visual C++ 6 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 7, 2002
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More
Without checkboxes. (Default) With checkboxes.

Environment: Visual C++ 6

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 include:

  • 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 CListBoxs 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:

   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:

Advertisement
  1. Add a listbox to your dialog’s resource.
  2. Set up your listbox’s resource for “Owner Draw: Fixed” and check “Has Strings”.
  3. Create a CCatListBox member variable in your dialog’s code. For example…
    #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
    }
    

Downloads

Download demo project – 20 Kb

Download source – 2 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.