Click here to Skip to main content
Licence CPOL
First Posted 8 Jun 2005
Views 81,591
Downloads 501
Bookmarked 39 times

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

By | 8 Jun 2005 | Article
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)

About the Author

Chen Hao

Web Developer

China China

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFix in DrawDragDivider() PinmemberThornets23:35 19 Dec '07  
Questionmultiple selection in menu Pinmembersharathgowda2:18 10 Jul '07  
GeneralAn issue though! PinmemberNibu babu thomas22:59 30 May '07  
GeneralPort to VC6! Pinmembercristitomi1:33 5 Apr '07  
GeneralRe: Port to VC6! PinmemberLeon Salters22:13 14 May '07  
GeneralRe: Port to VC6! Pinmembercristitomi5:03 17 May '07  
QuestionRe: Port to VC6! PinmemberLeon Salters1:09 4 Jun '07  
GeneralBug with column order PinmemberPaddy4781:29 30 Sep '06  
QuestionProblems to include functionalities to my project [modified] Pinmembercrisoc5:08 22 May '06  
AnswerRe: Problems to include functionalities to my project PinmemberFloppe2:04 11 Jul '06  
GeneralRe: Problems to include functionalities to my project Pinmembercrisoc2:20 11 Jul '06  
GeneralASSERT during creation within CView derived class PinmemberKeesR4:52 14 May '06  
GeneralWidth bug (with solution) PinmemberKeesR4:45 14 May '06  
Questionguidance needed to implement this feature for a Listview Pinmemberjaisri.anni22:22 7 May '06  
GeneralFix needed in header control PinmemberHogne Fossland21:56 5 Mar '06  
Questioneasier way? Pinmembervfilll6:14 2 Oct '05  
AnswerRe: easier way? PinmemberHelen_Bakulina5:20 3 May '06  
AnswerRe: easier way? PinmvpDavidCrow2:49 11 May '07  
GeneralCListCtrlEx Pinmemberphoenixwu3:41 27 Jun '05  
GeneralOT: You might think about replacing that image... PinmemberJohann Gerell23:43 9 Jun '05  
GeneralNice Pinmemberthe-unforgiven21:37 8 Jun '05  
GeneralRe: Nice PinmemberAlexander M.5:42 9 Jun '05  
GeneralRe: Nice PinmemberMMs_xH2:40 30 Jun '05  

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.

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