Microsoft KB Archive/214478

= How to handle check notifications in CCheckListBox in Visual C++ =

Article ID: 214478

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ 2005 Express Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q214478

<div class="summary_section">

SUMMARY
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

The CCheckListBox check event can be handled through the undocumented CLBN_CHKCHANGE notification message.

<div class="moreinformation_section">

MORE INFORMATION
CCheckListBox is an MFC control based on an owner drawn ListBox control. It consists of a list of items with a check box. An item can be selected by clicking inside the check box next to the text of the item. This generates a CLBN_CHKCHANGE notification message which is sent to the parent window (usually a dialog). The notification can be handled by adding a notification handler as shown below using a parent dialog window as an example: // header file class CChecktestDlg : public CDialog { protected: afx_msg void OnSelchangeList1; afx_msg void OnChkChange; ... };

// cpp file BEGIN_MESSAGE_MAP(CChecktestDlg, CDialog) ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1) ON_CONTROL(CLBN_CHKCHANGE, IDC_LIST1, OnChkChange) END_MESSAGE_MAP

void CChecktestDlg::OnSelchangeList1 {   TRACE("OnSelchangeList1 called\n"); }

void CChecktestDlg::OnChkChange {   TRACE("OnChkChange called\n"); }

The notification handler for LBN_SELCHANGE is also shown to demonstrate the difference between the two notifications. If the check state of an item is set, the item is also selected (clearing the check box does not cancel the item). However, changing the selection state does not affect the check state. Since there was no macro similar to ON_LBN_SELCHANGE, ON_CONTROL was used.

The behavior of the CCheckListBox was modified between Visual C++ version 5.0 and Visual C++ version 6.0. MFC in Visual C++ version 6.0 added the following method:

void SetSelectionCheck(int nCheck)

where nCheck has the same meaning in CCheckListBox::SetCheck:

<pre class="fixed_text"> 0 - clear 1 - checked 2 - indeterminate

SetSelectionCheck changes the check state of all selected items to the state of nCheck. When an item that is selected is checked in Visual C++ version 6.0, SetSelectionCheck changes the check state for all the other selected items. Keep this in mined when handling check notifications.

<div class="references_section">