Click here to Skip to main content
Licence CPOL
First Posted 27 Mar 2006
Views 77,059
Downloads 2,323
Bookmarked 59 times

Using Check Box in List Control

By | 27 Mar 2006 | Article
Using check box in both head ctrl and list control items, so you could easy operate it.
Sample Image - LVChecked.jpg

Introduction

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.

Steps

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();
ASSERT(pHeadCtrl->GetSafeHwnd());

VERIFY( m_checkHeadCtrl.SubclassWindow(pHeadCtrl->GetSafeHwnd()) );
VERIFY( m_checkImgList.Create(IDB_CHECKBOXES, 16, 3, RGB(255,0,255)));
int i = m_checkImgList.GetImageCount();
m_checkHeadCtrl.SetImageList(&m_checkImgList);
    
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)
    //{{AFX_MSG_MAP(CCheckHeadCtrl)
    ON_NOTIFY_REFLECT(HDN_ITEMCLICK, OnItemClicked)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

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

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

    if (hdItem.iImage == 1)
        hdItem.iImage = 2;
    else
        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)
    //{{AFX_MSG_MAP(CCheckListCtrl)
    ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemChanged)        
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCheckListCtrl message handlers
void CCheckListCtrl::OnItemChanged(NMHDR* pNMHDR, LRESULT* pResult)
{
    NMLISTVIEW* pNMLV = (NMLISTVIEW*)pNMHDR;
    *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;
                break;
            }
        }
        
        HDITEM hdItem;
        hdItem.mask = HDI_IMAGE;
        if (blAllChecked)
            hdItem.iImage = 2;
        else
            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.

History

  • 27th March, 2006: Initial post

License

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

About the Author

KeyL



United States United States

Member



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
GeneralOne Question PinmemberShilpi Boosar22:31 29 Nov '10  
Generalerror Pinmemberlocoone18:15 30 Jun '07  
GeneralRe: error PinmemberHoratiu Graur22:54 15 Jan '09  
GeneralDebug Assertion Failed error Pinmemberneha.agarwal272:22 2 May '07  
GeneralA remark! Pinmembercristitomi23:35 2 Apr '07  
GeneralRe: A remark! PinmemberPark Chunsoo19:10 24 Aug '08  
QuestionHow to add check boxes in multiple coloumns Pinmemberakvanama23:31 5 Sep '06  
AnswerRe: How to add check boxes in multiple coloumns PinmemberMember 386968220:44 8 Dec '07  
QuestionCompile problem, please help. PinmemberChanus6:01 2 Aug '06  
AnswerRe: Compile problem, please help. Pinmember12qw17:47 6 Nov '06  
GeneralThree-state checkbox PinmemberDominik Reichl23:22 30 Jun '06  
Generalgood work Pinmember.dan.g.11:53 28 Mar '06  

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
Web02 | 2.5.120517.1 | Last Updated 27 Mar 2006
Article Copyright 2006 by KeyL
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid