Click here to Skip to main content
15,943,798 members
Articles / Desktop Programming / MFC

Using Check Box in List Control

Rate me:
Please Sign up or sign in to vote.
4.71/5 (20 votes)
27 Mar 2006CPOL1 min read 178.8K   12.1K   77   15
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.

C++
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.

C++
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.

C++
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.

C++
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionadd DeInit Pin
qingye200826-Jul-19 0:59
qingye200826-Jul-19 0:59 
SuggestionHandling large lists Pin
rhfritz112-Jun-18 7:18
rhfritz112-Jun-18 7:18 
Questionhow to get row checked in list control ??? Pin
Member 133207879-Nov-17 16:42
Member 133207879-Nov-17 16:42 
GeneralOne Question Pin
ShilpiP29-Nov-10 22:31
ShilpiP29-Nov-10 22:31 
Generalerror Pin
locoone30-Jun-07 18:15
locoone30-Jun-07 18:15 
GeneralRe: error Pin
Horatiu Graur15-Jan-09 22:54
Horatiu Graur15-Jan-09 22:54 
GeneralDebug Assertion Failed error Pin
neha.agarwal272-May-07 2:22
neha.agarwal272-May-07 2:22 
GeneralA remark! Pin
cristitomi2-Apr-07 23:35
cristitomi2-Apr-07 23:35 
GeneralRe: A remark! Pin
Park Chunsoo24-Aug-08 19:10
Park Chunsoo24-Aug-08 19:10 
QuestionHow to add check boxes in multiple coloumns Pin
akvanama5-Sep-06 23:31
akvanama5-Sep-06 23:31 
AnswerRe: How to add check boxes in multiple coloumns Pin
Member 38696828-Dec-07 20:44
Member 38696828-Dec-07 20:44 
QuestionCompile problem, please help. Pin
Chanus2-Aug-06 6:01
Chanus2-Aug-06 6:01 
AnswerRe: Compile problem, please help. Pin
12qw6-Nov-06 17:47
12qw6-Nov-06 17:47 
GeneralThree-state checkbox Pin
Dominik Reichl30-Jun-06 23:22
Dominik Reichl30-Jun-06 23:22 
Generalgood work Pin
.dan.g.28-Mar-06 11:53
professional.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.