Add checkboxes | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 1, 1999
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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_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.
Advertisement

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.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.