Click here to Skip to main content
11,577,429 members (61,036 online)
Click here to Skip to main content

Adding Checkboxes to a list control

, 9 Jan 2000 258.2K 69
Rate this:
Please Sign up or sign in to vote.
Using the updated list control in IE3 and above to add check boxes to your list controls

A list view gives the GUI designer many options. One of the best is the ability to display tabular data in columns, sort columns, add images and more. This is well implemented by CListView in MFC.

A checked list box enables the GUI designer to get the users picked options via a checkbox on every list item. This is well implemented by CCheckListBox in MFC.

How to combine them both into one control?

Basically, there are two options: either you use owner drawn list view controls and draw your own check boxes as small images OR you can use the new control introduced in Microsoft's IE 3.0. To use the new features of a list view control you must install IE 3.0 (or above) to get the newest version of COMCTL32.DLL (the common controls library).

This control introduces some new flags in the ListView style and adds accepts some macros defined in the windows header files.

Bear in mind, the new flags and macros DO NOT APPEAR in VC++ 4.2 help files and started to exists in help files only from MSDN Jan 97 version and V++ 5.0

Well, here goes:

First, you have to set the new style in the list view control. This can be done by:

ListView_SetExtendedListViewStyle 
   (m_lvTestList.m_hWnd, LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);

This sets the list view to support check boxes and also a full row select (not only the 1st column).

LVS_EX_CHECKBOXES The control supplies check boxes for each item. You can retrieve the state of the check box by using the ListView_GetCheckState macro.
LVS_EX_FULLROWSELECT When an item is selected, all of its subitems are also displayed as elected. Clicking on any subitem will select the entire row. This extended style is only effective in conjunction with the LVS_REPORT style.
LVS_EX_GRIDLINES Dashed gridlines are displayed around all items and subitems. This extended style is only effective in conjunction with the LVS_REPORT style.
LVS_EX_HEADERDRAGDROP Enables drag-and-drop re-ordering of the columns in the ListView. This extended style is only effective in conjunction with the LVS_REPORT style.
LVS_EX_SUBITEMIMAGES Allows images to be displayed for subitems. This extended style is only effective in conjunction with the LVS_REPORT style.
LVS_EX_TRACKSELECT Enables hot tracking of items in a ListView control. Hot Tracking, also known as Hover Selection, means that an item is automatically selected when the mouse pointer is over it for more than 1 second. This style applies to all styles of the ListView control.

How to get notification when an item is checked / unchecked:

void DemoDlg::OnItemchangedLinksList(NMHDR* pNMHDR, LRESULT* pResult) 
{
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
    *pResult = 0;

    if (pNMListView->uOldState == 0 && pNMListView->uNewState == 0)
        return;    // No change


    // Old check box state
    BOOL bPrevState = (BOOL)(((pNMListView->uOldState & 
                LVIS_STATEIMAGEMASK)>>12)-1);  
    if (bPrevState < 0)    // On startup there's no previous state 
        bPrevState = 0; // so assign as false (unchecked)

    // New check box state
    BOOL bChecked = <BR>            (BOOL)(((pNMListView->uNewState & LVIS_STATEIMAGEMASK)>>12)-1);   
    if (bChecked < 0) // On non-checkbox notifications assume false
        bChecked = 0; 

    if (bPrevState == bChecked) // No change in check box
        return;
    
    // Now bChecked holds the new check box state

    // ....
}

for this to work, you must map the following message:

ON_NOTIFY(LVN_ITEMCHANGED, IDC_MYLIST, OnItemchangedLinksList)

Setting the check box state of an item:

Try the following piece of code

void SetLVCheck (WPARAM ItemIndex, BOOL bCheck)
{
    ListView_SetItemState (m_lvTestList.m_hWnd, ItemIndex, 
        UINT((int(bCheck) + 1) << 12), LVIS_STATEIMAGEMASK);
}

Getting the check box state of an item:

Use the macro ListView_GetCheckState(hwndLV, i) defined in commctl.h (hwndLV is the window handle of the list view member - i.e, m_lvTestList.m_hWnd and i is the list view index)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Eran Yariv
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
AnswerRe: How to disable check box within ctrlList Pin
lauren26-Mar-01 0:09
memberlauren26-Mar-01 0:09 
GeneralI see, you are the smart one... Pin
Pav2-May-01 7:01
memberPav2-May-01 7:01 
GeneralRe: I see, you are the smart one... Pin
lauren2-May-01 11:23
memberlauren2-May-01 11:23 
GeneralRe: I see, you are the smart one... Pin
Pav3-May-01 4:54
memberPav3-May-01 4:54 
GeneralRe: How to disable check box within ctrlList Pin
Anna-Jayne Metcalfe10-Jun-04 3:29
memberAnna-Jayne Metcalfe10-Jun-04 3:29 
GeneralRe: How to disable check box within ctrlList Pin
magenof25-Jan-07 9:12
membermagenof25-Jan-07 9:12 
GeneralRe: How to disable check box within ctrlList Pin
Bzepp19-Jun-07 12:11
memberBzepp19-Jun-07 12:11 
AnswerRe: How to disable check box within ctrlList Pin
Dave Loeser5-Jun-03 8:48
memberDave Loeser5-Jun-03 8:48 
AnswerRe: How to disable check box within ctrlList Pin
mace28-Apr-05 3:48
membermace28-Apr-05 3:48 
GeneralCheck boxes and Images in subItems Pin
Anonymous18-Nov-00 23:08
memberAnonymous18-Nov-00 23:08 
QuestionHow to put check box in 2nd column Pin
Jean Laborde27-Jun-00 3:49
sussJean Laborde27-Jun-00 3:49 
AnswerRe: How to put check box in 2nd column Pin
Karl-Heinz Pfueller13-Jul-00 2:23
sussKarl-Heinz Pfueller13-Jul-00 2:23 
GeneralRe: How to put check box in 2nd column Pin
yaoyj3-Nov-02 20:15
sussyaoyj3-Nov-02 20:15 
GeneralRe: How to put check box in 2nd column Pin
dvid6-Mar-03 16:05
memberdvid6-Mar-03 16:05 
GeneralFining out which items are "checked". Pin
Quoc18-Jun-00 16:43
sussQuoc18-Jun-00 16:43 
GeneralRe: Fining out which items are checked Pin
Robin Dunlop4-Jul-00 22:39
sussRobin Dunlop4-Jul-00 22:39 
GeneralRe: Fining out which items are checked Pin
kiranin19-May-06 0:06
memberkiranin19-May-06 0:06 
QuestionDoes it work with virtual listctrl (LVS_OWNERDATA) ? Pin
Emmanuel Derriey2-Apr-00 21:11
sussEmmanuel Derriey2-Apr-00 21:11 
AnswerRe: Does it work with virtual listctrl (LVS_OWNERDATA) ? Pin
Anonymous22-Apr-04 10:56
sussAnonymous22-Apr-04 10:56 
QuestionSample for Owner-Drawn Checkboxes ? Pin
Michael Krebs12-Jan-00 4:27
sussMichael Krebs12-Jan-00 4:27 
AnswerRe: Sample for Owner-Drawn Checkboxes ? Pin
Mike Dunn12-Jan-00 19:50
sussMike Dunn12-Jan-00 19:50 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 10 Jan 2000
Article Copyright 2000 by Eran Yariv
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid