Click here to Skip to main content
11,704,107 members (66,458 online)
Click here to Skip to main content

An effective way to hide or show columns in list control dynamically

, 8 Jun 2005 CPOL 96.5K 1.2K 43
Rate this:
Please Sign up or sign in to vote.
An article on how to dynamically hide columns in a list control.

CListCtrlEx Demo

Introduction

Recently, in one of my projects, I needed a CListCtrl derived class which has the ability to show/hide its columns dynamically. Using CListCtrl::DeleteColumn() is not a good idea because in that case the index of the sub items will be changed from time to time, thus assessing the sub items will become very complicated. I searched the Internet and did not find an existing solution for this. At last, I had to write my own class - CListCtrlEx to implement the feature.

The main features of CListCtrlEx

  • Dynamically hide/show the columns.
  • Provide a built-in context menu to select the columns and it is dynamically created based on the columns you insert.
  • Drag-and-drop reorder columns (you must enable the LVS_EX_HEADERDRAGDROP style).
  • Save/restore the columns' order, width and visibility in/from registry automatically.
  • You do not need to change your existing code of CListCtrl derived classes. All you have to do is change their base class to CListCtrlEx.

Using the CListCtrlEx class

  1. Add the following files to your project:
    • HeaderCtrlEx.h
    • HeaderCtrlEx.cpp
    • ListCtrlEx.h
    • ListCtrlEx.cpp
  2. Import the following two cursor files into you resource and change their ID as mentioned in the square brackets:
    • res\VE_SPLIT.CUR [IDC_HEADER_OPEN]
    • res\VE_SIZEB.CUR [IDC_HEADER_SIZE]
  3. Derive your own class, say CMyListCtrl, from CListCtrlEx.
  4. Add a list control to your dialog in the resource editor and bind it to a member variable, say m_wndList. Of course the variable should be a CMyListCtrl object.
    void CTestListCtrlDlg::DoDataExchange(CDataExchange* pDX)
    {
        CDialog::DoDataExchange(pDX);
        DDX_Control(pDX, IDC_LIST, m_wndList);
    }
  5. Save/restore the state of the columns.
    BOOL CTestListCtrlDlg::OnInitDialog()
    {
        CDialog::OnInitDialog();
    
        // Enable column drag & drop
        m_wndList.SetExtendedStyle(m_wndList.GetExtendedStyle() 
                                      | LVS_EX_HEADERDRAGDROP);
    
        // Insert columns and items to the list control here
    
        // Set the sub registry key to store the columns' state
        m_wndList.SetRegistryKey("MyListCtrl");
    
        // Restore the state of the columns
        m_wndList.RestoreState();
    }
    
    void CTestListCtrlDlg::OnDestroy()
    {
        CDialog::OnDestroy();
    
        // TODO: Add your message handler code here
    
        // Save the state of the columns
        m_wndList.SaveState();
    }

License

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

Share

About the Author

Chen Hao
Web Developer
China China
Chen Hao is a programmer of SourceTec Software Co., LTD.
He began to program since 1992 and programming became
one of the most important things in his life since then.

His most recent projects were Sothink SWF Quicker and
Sothink SWF Decompiler.

You may also be interested in...

Comments and Discussions

 
QuestionChange font of a menu? Pin
bosfan4-Oct-13 1:43
memberbosfan4-Oct-13 1:43 
GeneralFix in DrawDragDivider() Pin
Thornets19-Dec-07 23:35
memberThornets19-Dec-07 23:35 
Questionmultiple selection in menu Pin
sharathgowda10-Jul-07 2:18
membersharathgowda10-Jul-07 2:18 
GeneralAn issue though! Pin
Nibu babu thomas30-May-07 22:59
memberNibu babu thomas30-May-07 22:59 
GeneralPort to VC6! Pin
cristitomi5-Apr-07 1:33
membercristitomi5-Apr-07 1:33 
GeneralRe: Port to VC6! Pin
Leon Salters14-May-07 22:13
memberLeon Salters14-May-07 22:13 
GeneralRe: Port to VC6! Pin
cristitomi17-May-07 5:03
membercristitomi17-May-07 5:03 
QuestionRe: Port to VC6! Pin
Leon Salters4-Jun-07 1:09
memberLeon Salters4-Jun-07 1:09 
GeneralBug with column order Pin
Paddy47830-Sep-06 1:29
memberPaddy47830-Sep-06 1:29 
QuestionProblems to include functionalities to my project [modified] Pin
crisoc22-May-06 5:08
membercrisoc22-May-06 5:08 
AnswerRe: Problems to include functionalities to my project Pin
Floppe11-Jul-06 2:04
memberFloppe11-Jul-06 2:04 
GeneralRe: Problems to include functionalities to my project Pin
crisoc11-Jul-06 2:20
membercrisoc11-Jul-06 2:20 
GeneralASSERT during creation within CView derived class Pin
KeesR14-May-06 4:52
memberKeesR14-May-06 4:52 
GeneralWidth bug (with solution) Pin
KeesR14-May-06 4:45
memberKeesR14-May-06 4:45 
Questionguidance needed to implement this feature for a Listview Pin
jaisri.anni7-May-06 22:22
memberjaisri.anni7-May-06 22:22 
GeneralFix needed in header control Pin
Hogne Fossland5-Mar-06 21:56
memberHogne Fossland5-Mar-06 21:56 
Questioneasier way? Pin
vfilll2-Oct-05 6:14
membervfilll2-Oct-05 6:14 
AnswerRe: easier way? Pin
Helen_Bakulina3-May-06 5:20
memberHelen_Bakulina3-May-06 5:20 
AnswerRe: easier way? Pin
DavidCrow11-May-07 2:49
mvpDavidCrow11-May-07 2:49 
GeneralCListCtrlEx Pin
phoenixwu27-Jun-05 3:41
memberphoenixwu27-Jun-05 3:41 
GeneralOT: You might think about replacing that image... Pin
Johann Gerell9-Jun-05 23:43
memberJohann Gerell9-Jun-05 23:43 
GeneralNice Pin
the-unforgiven8-Jun-05 21:37
memberthe-unforgiven8-Jun-05 21:37 
GeneralRe: Nice Pin
Alexander M.9-Jun-05 5:42
memberAlexander M.9-Jun-05 5:42 
GeneralRe: Nice Pin
MMs_xH30-Jun-05 2:40
memberMMs_xH30-Jun-05 2:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 9 Jun 2005
Article Copyright 2005 by Chen Hao
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid