Click here to Skip to main content
11,929,249 members (57,207 online)
Click here to Skip to main content
Add your own
alternative version


74 bookmarked

Using Check Box in List Control

, 27 Mar 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Using check box in both head ctrl and list control items, so you could easy operate it.
Sample Image - LVChecked.jpg


It's easy to add check box in List Control, but the check box is only added in the list's items, not in the head items, and also you could not select or de-select all items in the list by one hit.

This article discusses a method which adds a check box in head items, and could use this check box to select or de-select all items in the list.


1. Adding Check Box for List Control

It's fairly easy. Just set the extended style: LVS_EX_CHECKBOXES.

m_listCtrl.SetExtendedStyle( m_listCtrl.GetExtendedStyle() | LVS_EX_CHECKBOXES);

2. Adding Check Box for Head Ctrl

First, we must create a Image list which contains the check box bitmap, and then set the image list to headctrl, finally we need to set the image for first head item using CHeadCtrl::setItem.

CHeaderCtrl* pHeadCtrl = this->GetHeaderCtrl();

VERIFY( m_checkHeadCtrl.SubclassWindow(pHeadCtrl->GetSafeHwnd()) );
VERIFY( m_checkImgList.Create(IDB_CHECKBOXES, 16, 3, RGB(255,0,255)));
int i = m_checkImgList.GetImageCount();
HDITEM hdItem;
hdItem.mask = HDI_IMAGE | HDI_FORMAT;
VERIFY( m_checkHeadCtrl.GetItem(0, &hdItem) );
hdItem.iImage = 1;
hdItem.fmt |= HDF_IMAGE;
VERIFY( m_checkHeadCtrl.SetItem(0, &hdItem) );

3. Using Check Box in headctrl to Select or De-select All Items in List

We need to respond to the HDN_ITEMCLICK notification message of headctrl by either notify or notify reflect mechanism. I choose notify reflect.

BEGIN_MESSAGE_MAP(CCheckHeadCtrl, CHeaderCtrl)
    // NOTE - the ClassWizard will add and remove mapping macros here.

// CCheckHeadCtrl message handlers
void CCheckHeadCtrl::OnItemClicked(NMHDR* pNMHDR, LRESULT* pResult)
    *pResult = 0;

    int nItem = pNMHead->iItem;
    if (0 != nItem)

    HDITEM hdItem;
    hdItem.mask = HDI_IMAGE;
    VERIFY( GetItem(nItem, &hdItem) );

    if (hdItem.iImage == 1)
        hdItem.iImage = 2;
        hdItem.iImage = 1;

    VERIFY( SetItem(nItem, &hdItem) );
    BOOL bl = hdItem.iImage == 2 ? TRUE : FALSE;
    CListCtrl* pListCtrl = (CListCtrl*)GetParent();
    int nCount = pListCtrl->GetItemCount();    
    for(nItem = 0; nItem < nCount; nItem++)
        ListView_SetCheckState(pListCtrl->GetSafeHwnd(), nItem, bl);

4. Change the Head's Check Status When the Check Status Changed in List Control

We need to respond to the notification message LVN_ITEMCHANGED of list control.

BEGIN_MESSAGE_MAP(CCheckListCtrl, CListCtrl)

// CCheckListCtrl message handlers
void CCheckListCtrl::OnItemChanged(NMHDR* pNMHDR, LRESULT* pResult)
    *pResult = 0;

    if ( m_blInited && LVIF_STATE == pNMLV->uChanged)
        BOOL blAllChecked = TRUE;
        int nCount = GetItemCount();
        for(int nItem = 0; nItem < nCount; nItem++)
            if ( !ListView_GetCheckState(GetSafeHwnd(), nItem) )
                blAllChecked = FALSE;
        HDITEM hdItem;
        hdItem.mask = HDI_IMAGE;
        if (blAllChecked)
            hdItem.iImage = 2;
            hdItem.iImage = 1;
        VERIFY( m_checkHeadCtrl.SetItem(0, &hdItem) );

Demo Project

It is a dialog based application that covers all I mentioned in this article. You could inspect it in your environment.


  • 27th March, 2006: Initial post


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralOne Question Pin
Shilpi Boosar29-Nov-10 23:31
memberShilpi Boosar29-Nov-10 23:31 
Generalerror Pin
locoone30-Jun-07 19:15
memberlocoone30-Jun-07 19:15 
GeneralRe: error Pin
Horatiu Graur15-Jan-09 23:54
memberHoratiu Graur15-Jan-09 23:54 
GeneralDebug Assertion Failed error Pin
neha.agarwal272-May-07 3:22
memberneha.agarwal272-May-07 3:22 
GeneralA remark! Pin
cristitomi3-Apr-07 0:35
membercristitomi3-Apr-07 0:35 
GeneralRe: A remark! Pin
Park Chunsoo24-Aug-08 20:10
memberPark Chunsoo24-Aug-08 20:10 
QuestionHow to add check boxes in multiple coloumns Pin
akvanama6-Sep-06 0:31
memberakvanama6-Sep-06 0:31 
AnswerRe: How to add check boxes in multiple coloumns Pin
Member 38696828-Dec-07 21:44
memberMember 38696828-Dec-07 21:44 
QuestionCompile problem, please help. Pin
Chanus2-Aug-06 7:01
memberChanus2-Aug-06 7:01 
AnswerRe: Compile problem, please help. Pin
12qw6-Nov-06 18:47
member12qw6-Nov-06 18:47 
GeneralThree-state checkbox Pin
Dominik Reichl1-Jul-06 0:22
memberDominik Reichl1-Jul-06 0:22 
Generalgood work Pin
.dan.g.28-Mar-06 12:53
member.dan.g.28-Mar-06 12:53 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 27 Mar 2006
Article Copyright 2006 by KeyL
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid