Click here to Skip to main content
12,886,364 members (44,224 online)
Click here to Skip to main content
Add your own
alternative version

Stats

128.6K views
9K downloads
75 bookmarked
Posted 27 Mar 2006

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

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)

Share

About the Author

KeyL
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralOne Question Pin
Shilpi Boosar29-Nov-10 22:31
memberShilpi Boosar29-Nov-10 22:31 
Generalerror Pin
locoone30-Jun-07 18:15
memberlocoone30-Jun-07 18:15 
GeneralRe: error Pin
Horatiu Graur15-Jan-09 22:54
memberHoratiu Graur15-Jan-09 22:54 
GeneralDebug Assertion Failed error Pin
neha.agarwal272-May-07 2:22
memberneha.agarwal272-May-07 2:22 
GeneralA remark! Pin
cristitomi2-Apr-07 23:35
membercristitomi2-Apr-07 23:35 
GeneralRe: A remark! Pin
Park Chunsoo24-Aug-08 19:10
memberPark Chunsoo24-Aug-08 19:10 
QuestionHow to add check boxes in multiple coloumns Pin
akvanama5-Sep-06 23:31
memberakvanama5-Sep-06 23:31 
AnswerRe: How to add check boxes in multiple coloumns Pin
Member 38696828-Dec-07 20:44
memberMember 38696828-Dec-07 20:44 
QuestionCompile problem, please help. Pin
Chanus2-Aug-06 6:01
memberChanus2-Aug-06 6:01 
AnswerRe: Compile problem, please help. Pin
12qw6-Nov-06 17:47
member12qw6-Nov-06 17:47 
GeneralThree-state checkbox Pin
Dominik Reichl30-Jun-06 23:22
memberDominik Reichl30-Jun-06 23:22 
Generalgood work Pin
.dan.g.28-Mar-06 11:53
member.dan.g.28-Mar-06 11: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 27 Mar 2006
Article Copyright 2006 by KeyL
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid