转自:http://www.codeproject.com/kb/combobox/CCheckListBoxUsage.aspx
Introduction
I like the CCheckListBox
class provided with MFC, however, it�s 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).
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.
In the header code, change the wizard generated code from:
Collapse
enum
{ IDD = IDD_CHECKLISTBOXCBN_DIALOG };
CListBox m_ctlCheckList;
to:
Collapse
enum
{ IDD = IDD_CHECKLISTBOXCBN_DIALOG };
CCheckListBox m_ctlCheckList;
In the body, change the following generated code from:
Collapse void
CCheckListBoxCBNDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_ctlCheckList);
}
to:
Collapse void
CCheckListBoxCBNDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
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:
Collapse m_ctlCheckList.ResetContent();
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:
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.
Collapse
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();
In the body, add the event handler to the message map:
Collapse BEGIN_MESSAGE_MAP(CCheckListBoxCBNDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
ON_CLBN_CHKCHANGE(IDC_LIST1, OnCheckchangeList1)
END_MESSAGE_MAP()
And add your implementation of the handler.
Collapse void
CCheckListBoxCBNDlg::OnCheckchangeList1()
{
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.