Click here to Skip to main content
15,867,594 members
Articles / Desktop Programming / MFC

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

Rate me:
Please Sign up or sign in to vote.
4.68/5 (15 votes)
8 Jun 2005CPOL1 min read 124.5K   1.7K   44   24
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)


Written By
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.

Comments and Discussions

 
QuestionChange font of a menu? Pin
bosfan4-Oct-13 1:43
bosfan4-Oct-13 1:43 
GeneralFix in DrawDragDivider() Pin
Thornets19-Dec-07 23:35
Thornets19-Dec-07 23:35 
Questionmultiple selection in menu Pin
sharathgowda10-Jul-07 2:18
sharathgowda10-Jul-07 2:18 
GeneralAn issue though! Pin
Nibu babu thomas30-May-07 22:59
Nibu babu thomas30-May-07 22:59 
GeneralPort to VC6! Pin
cristitomi5-Apr-07 1:33
cristitomi5-Apr-07 1:33 
GeneralRe: Port to VC6! Pin
Leon Salters14-May-07 22:13
Leon Salters14-May-07 22:13 
GeneralRe: Port to VC6! Pin
cristitomi17-May-07 5:03
cristitomi17-May-07 5:03 
QuestionRe: Port to VC6! Pin
Leon Salters4-Jun-07 1:09
Leon Salters4-Jun-07 1:09 
GeneralBug with column order Pin
Paddy47830-Sep-06 1:29
Paddy47830-Sep-06 1:29 
Try running TestListCtrl.exe in the demo project with the columns arranged

Col 0, Col 1, Col 2, Col 3, Col 4, Col 5

(Don't change the order of the columns using drag/drop)

Now right click on the header, and remove the tick from Col 1.
Next, remove tick from Col 2
Next, remove tick from Col 3
Next, remove tick from Col 4

Now put the tick back in Col 4.

The columns are now arranged in the wrong order. Col 4 is to the right of Col 5, but it should be on the left.

QuestionProblems to include functionalities to my project [modified] Pin
crisoc22-May-06 5:08
crisoc22-May-06 5:08 
AnswerRe: Problems to include functionalities to my project Pin
Floppe11-Jul-06 2:04
Floppe11-Jul-06 2:04 
GeneralRe: Problems to include functionalities to my project Pin
crisoc11-Jul-06 2:20
crisoc11-Jul-06 2:20 
GeneralASSERT during creation within CView derived class Pin
KeesR14-May-06 4:52
KeesR14-May-06 4:52 
GeneralWidth bug (with solution) Pin
KeesR14-May-06 4:45
KeesR14-May-06 4:45 
Questionguidance needed to implement this feature for a Listview Pin
jaisri.anni7-May-06 22:22
jaisri.anni7-May-06 22:22 
GeneralFix needed in header control Pin
Hogne Fossland5-Mar-06 21:56
Hogne Fossland5-Mar-06 21:56 
Questioneasier way? Pin
vfilll2-Oct-05 6:14
vfilll2-Oct-05 6:14 
AnswerRe: easier way? Pin
Helen_Bakulina3-May-06 5:20
Helen_Bakulina3-May-06 5:20 
AnswerRe: easier way? Pin
David Crow11-May-07 2:49
David Crow11-May-07 2:49 
GeneralCListCtrlEx Pin
phoenixwu27-Jun-05 3:41
phoenixwu27-Jun-05 3:41 
GeneralOT: You might think about replacing that image... Pin
Johann Gerell9-Jun-05 23:43
Johann Gerell9-Jun-05 23:43 
GeneralNice Pin
the-unforgiven8-Jun-05 21:37
the-unforgiven8-Jun-05 21:37 
GeneralRe: Nice Pin
Alexander M.,9-Jun-05 5:42
Alexander M.,9-Jun-05 5:42 
GeneralRe: Nice Pin
MMs_xH30-Jun-05 2:40
MMs_xH30-Jun-05 2:40 

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.