Add checkboxes

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).

A full description of the new flags follows in this table:

LVS_EX_CHECKBOXESThe control supplies check boxes for each item. You can retrieve the state of the check box by using the ListView_GetCheckState macro.
LVS_EX_FULLROWSELECTWhen 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_GRIDLINESDashed gridlines are displayed around all items and subitems. This extended style is only effective in conjunction with the LVS_REPORT style.
LVS_EX_HEADERDRAGDROPEnables 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_SUBITEMIMAGESAllows images to be displayed for subitems. This extended style is only effective in conjunction with the LVS_REPORT style.
LVS_EX_TRACKSELECTEnables 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

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

	// New check box state
	BOOL bChecked=(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)

Email address for Eran Yariv updated on: April 11, 98.



Comments

  • Help plz

    Posted by JMJ on 08/14/2012 02:07am

    I have created a listview with checkbox along with text ,but i need checkbox to be selected when i click text too

    Reply
  • Shift-select in Clistctrl with checkboxes

    Posted by shrutzin on 12/27/2007 04:29am

    I have made a List control dialog box with checkboxes. i want that when all the elements of the list are selected using shift and one of the checkboxes is clicked the rest of the selected ones should also be clicked. What should i add in my code for this to happen?

    Reply
  • How can i add checkbox Control in to ListView

    Posted by phanduyduong on 03/31/2006 02:36pm

    How can i add checkbox Control in to ListView

    Reply
  • Howto center checkbox ?

    Posted by Legacy on 12/05/2003 12:00am

    Originally posted by: Gary Kettunen

    Hi all!

    A want to center checkbox when using wide column, because it looks wery worth when chekbox lies in left side of column.

    Can enyone help me?

    Reply
  • Getting Items which are checked in a list box with check box

    Posted by Legacy on 09/12/2003 12:00am

    Originally posted by: shashi

    int i =m_pCheckListBox.GetNextItem(-1, LVNI_ALL);
    
    while( i != -1 )
    {
    if(m_pCheckListBox.GetCheck(i) != 0)
    {
    TRACE("%d\n", i );
    // you can add your own code here
    }
    i = m_pCheckListBox.GetNextItem( i, LVNI_ALL);
    }

    Reply
  • I want partly checkbox in one column

    Posted by Legacy on 03/29/2003 12:00am

    Originally posted by: sandmanq

    I am poor English, Sorry.
    
    I create listctrl with checkbox.
    checkbox is made full in one column.
    I want partly checkbox in one column.
    How?:)

    Reply
  • How to get click the checkbox from a database??

    Posted by Legacy on 01/23/2003 12:00am

    Originally posted by: JingHong

    I would like to know how to get the checkbox click with the information that i get from a textfile
    Can anyone help??

    Reply
  • problem w/ checkboxes using comctl32.dll ver. 6.0

    Posted by Legacy on 04/08/2002 12:00am

    Originally posted by: Dave Bevan

    i've been using checkboxes on my list control as described above and it works great. my problem is that we've switched to MFC70 (Visual Studio .NET) and added the manifest resource to use common controls 6.0 when available. my problem is that on XP (which ships with 6.0) when an item in my control is selected, the checkbox is not drawn for that item. is anyone else having this problem? it's quite irritating and i may just have to go back to using comm ctrls 5.0, but i would like to support the new XP visual styles if possible.

    dave

    Reply
  • Disabling a Checkbox

    Posted by Legacy on 12/10/2001 12:00am

    Originally posted by: Tamer Omari

    cool, but is there any way to disable a checkbox, thats to appear disabled and not notifying when clicked?

    Reply
  • Ex(Properties)List

    Posted by Legacy on 10/07/2001 12:00am

    Originally posted by: Mike Philis

    Do you need an advanced list control? Check this out:
    
    http://www.exontrol.com/sg.jsp?content=products/exlist

    Mike
    www.exontrol.com

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds