Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / MFC

Using the CCheckListBox and Getting Check State Notification

Rate me:
Please Sign up or sign in to vote.
4.46/5 (29 votes)
4 May 20043 min read 211.9K   3.9K   64   25
Using the CCheckListBox and getting check state notification

Introduction

I like the CCheckListBox class provided with MFC, however, its use isn’t obvious and the wizard assistance stops at the CListBox class. I’ll describe how to insert it easily in your project. (There may be easier ways to do it, but this is how I do it and it works.)

Image 1

I’ll also show how you can add event notification so that you can find out when the check box state changes.

Creating the CCheckListBox Member

  • Create a new MFC Application or Dialog Application.
  • In the resource editor, add a "regular" list box to the dialog.
    • Right click the listbox properties, then the Styles tab
    • Ensure the Owner Draw = Fixed
    • Ensure Has Strings = checked
  • Holding the CTRL key, double click on the listbox in the resource dialog.

The wizard will only give you the option to create it as a CListBox, choose that, we will change it in code.

Image 2

In the header code, change the wizard generated code from:

C++
// Dialog Data
     //{{AFX_DATA(CCheckListBoxCBNDlg)
     enum { IDD = IDD_CHECKLISTBOXCBN_DIALOG };
     CListBox  m_ctlCheckList;
     //}}AFX_DATA

to:

C++
// Dialog Data
     //{{AFX_DATA(CCheckListBoxCBNDlg)
     enum { IDD = IDD_CHECKLISTBOXCBN_DIALOG };
     //}}AFX_DATA
CCheckListBox m_ctlCheckList;

In the body, change the following generated code from:

C++
void CCheckListBoxCBNDlg::DoDataExchange(CDataExchange* pDX)
{
     CDialog::DoDataExchange(pDX);
     //{{AFX_DATA_MAP(CCheckListBoxCBNDlg)
     DDX_Control(pDX, IDC_LIST1, m_ctlCheckList);
     //}}AFX_DATA_MAP
}

to:

C++
void CCheckListBoxCBNDlg::DoDataExchange(CDataExchange* pDX)
{
     CDialog::DoDataExchange(pDX);
     //{{AFX_DATA_MAP(CCheckListBoxCBNDlg)
     //}}AFX_DATA_MAP
     DDX_Control(pDX, IDC_LIST1, m_ctlCheckList);
}

Adding Items to the CCheckListBox

Now, you can add stuff to the checklist in your OnInitDialog member, like:

C++
     m_ctlCheckList.ResetContent();
//   m_ctlCheckList.SetCheckStyle( BS_AUTO3STATE );
     m_ctlCheckList.SetCheckStyle( BS_3STATE );
     m_ctlCheckList.AddString("Fumble");
     m_ctlCheckList.SetCheck( 0, 0 );
     m_ctlCheckList.AddString("Bumble");
     m_ctlCheckList.SetCheck( 1, 1 );
     m_ctlCheckList.AddString("Gumble");
     m_ctlCheckList.SetCheck( 2, 2 );

Note that the MSDN documentation is a little flimsy when it comes to the description of BS_AUTO3STATE and BS_3STATE. If you use BS_3STATE, then you will not get check box notifications and the states are locked (changeable in code only). If you use BS_AUTO3STATE, then you will get notifications of state changes, and the check boxes will manage themselves. You will just have to experiment with them to see which one gives you the effect you want.

Determining Check Box State Changes

You can still use the wizard for the check list control you’ve created, but you’ll see that the list is limited to CListBox specific items:

Image 3

I wanted a handler to know when a check box state changed (not a selection change). To accomplish this, manually add an event handler in the header as shown below. Note that if the user clicks on a check box, you will get two events for the click, first, OnCheckchangeList1, followed by OnSelchangeList1.

Caution: This is important if you depend on the current selection to change the checkbox state in a structure, i.e., the call to GetCurSel will be the new selection in the OnCheck call, even though OnSelchange hasn’t been called.

C++
//{{AFX_MSG(CCheckListBoxCBNDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnSelchangeList1();
afx_msg void OnCheckchangeList1();
//}}AFX_MSG

In the body, add the event handler to the message map:

C++
BEGIN_MESSAGE_MAP(CCheckListBoxCBNDlg, CDialog)
     //{{AFX_MSG_MAP(CCheckListBoxCBNDlg)
     ON_WM_SYSCOMMAND()
     ON_WM_PAINT()
     ON_WM_QUERYDRAGICON()
     ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
     //}}AFX_MSG_MAP
     ON_CLBN_CHKCHANGE(IDC_LIST1, OnCheckchangeList1)
END_MESSAGE_MAP()

And add your implementation of the handler.

C++
void CCheckListBoxCBNDlg::OnCheckchangeList1() 
{
     // TODO: Add your control notification handler code here
     TRACE( "CCheckListBoxCBNDlg::OnCheckchangeList1\n" );     
}

Conclusion

At this point, you have a check list box that you can easily extend. Several other CodeProject articles show multi check list box classes and list view report views with check boxes. This is the simplest implementation of the MFC CCheckListBox.

Some people do not like the CCheckListBox because it leads to some ambiguity, but it really depends on the context it’s used in. For example, does checking the item turn the thing on or does the thing get enabled. When does it get turned on, when I check it or when I press OK/Apply in the dialog. Use this control with caution.

The code in this article has minimal error checking, and is meant for illustrative purposes only.

Project has been tested on VC++ 6.0 SP5, Win 2K SP4.

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.


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Andrew Truckle28-Feb-20 8:56
professionalAndrew Truckle28-Feb-20 8:56 
QuestionVote of 5 Pin
leo.vannini.numecent10-Jun-15 6:59
leo.vannini.numecent10-Jun-15 6:59 
QuestionMy checkedListBox is disable in Toolbox Pin
Mạnh Lê16-Nov-14 23:40
Mạnh Lê16-Nov-14 23:40 
GeneralMy vote of 5 Pin
Torsten S21-Mar-11 11:57
Torsten S21-Mar-11 11:57 
GeneralWS_HSCROLL not working for CCheckListBox Pin
Sureshkotakng12-Jan-10 9:57
Sureshkotakng12-Jan-10 9:57 
GeneralCCheckListBox empty when MFC linked statically Pin
jerome2812-Jan-09 23:41
jerome2812-Jan-09 23:41 
GeneralVery Good ..thank u! Pin
Pragati Nijampurkar22-Dec-08 20:06
Pragati Nijampurkar22-Dec-08 20:06 
GeneralGood Pin
cxsoar@126.com27-Aug-08 22:12
cxsoar@126.com27-Aug-08 22:12 
GeneralVery Good Pin
Sayyed Mostafa Hashemi14-Nov-07 3:46
Sayyed Mostafa Hashemi14-Nov-07 3:46 
GeneralVery Good Pin
Sayyed Mostafa Hashemi14-Nov-07 3:41
Sayyed Mostafa Hashemi14-Nov-07 3:41 
QuestionGet a mistake when I usre bigger Type size Pin
yummi27-Jul-06 4:28
yummi27-Jul-06 4:28 
GeneralGreat!!! OnCheck works perfectly!! Pin
mimosa1-May-06 23:51
mimosa1-May-06 23:51 
QuestionWhy not use CLBN_CHKCHANGE? Pin
Anonymous4-Aug-05 1:53
Anonymous4-Aug-05 1:53 
AnswerRe: Why not use CLBN_CHKCHANGE? Pin
intensely_radioactive4-Aug-05 3:50
intensely_radioactive4-Aug-05 3:50 
GeneralDoesn't get all notification Pin
dmitchelli4-Mar-05 11:00
dmitchelli4-Mar-05 11:00 
GeneralRe: Doesn't get all notification Pin
intensely_radioactive4-Mar-05 17:30
intensely_radioactive4-Mar-05 17:30 
GeneralSome with checks, some with out Pin
Raymond S14-Jun-04 7:19
Raymond S14-Jun-04 7:19 
Generalbravo Raymond S Pin
jp dai31-Jan-05 12:37
jp dai31-Jan-05 12:37 
QuestionHas Strings? Pin
Nynaeve12-May-04 7:28
Nynaeve12-May-04 7:28 
AnswerRe: Has Strings? Pin
Nynaeve12-May-04 8:40
Nynaeve12-May-04 8:40 
Generalgreat!!! Pin
Eliane12-May-04 1:59
professionalEliane12-May-04 1:59 
GeneralDidn't work :( Pin
alex.barylski4-May-04 8:35
alex.barylski4-May-04 8:35 
GeneralRe: Didn't work :( Pin
intensely_radioactive4-May-04 12:16
intensely_radioactive4-May-04 12:16 
GeneralRe: Didn't work :( Pin
Brad Bruce29-May-04 10:39
Brad Bruce29-May-04 10:39 
GeneralRe: Didn't work :( Pin
dlubk24-Jan-15 3:29
dlubk24-Jan-15 3:29 

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.