CListCtrl is a very useful control for reporting purposes. It provides an option, which can be used to display a big report in a grid. If there is a requirement for me to select multiple rows for manipulating the data, one will immediately think of implementing a multi-select solution. But though implementing multi-select option in CListCtrl can be a solution, it can also be cumbersome for users. They will have to select the rows by pressing either CTRL key or the SHIFT keys. If they want to modify the selection or remove some, the user interface will not be so easier.
As an alternative, implementing a checked list control will be a favored solution. All the rows will be pre-fixed with Check boxes. If a user wants to select a row, he can tick/select by clicking inside the check box. This solution will provide a very user friendly interface. This article explains how to use Check boxes in a list control.
Implementing a CListCtrl with Check boxes:
- This article assumes that an MFC application is already created with a dialog box and a list control.
- The Resource Identifier for the List control is assumed to be IDC_LISTCONTROLSAMPLE.
- Use the class wizard and add a member variable for the list control IDC_LISTCONTROLSAMPLE. For the sample purposes the variable for CListCtrl is considered as m_lcSample.
- Modify the OnInitDialog() function of the Dialog box as in the following code snippet.
Sample Initialization code for CListCtrl with check boxes:
BOOL SampleDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization for CListCtrl here m_lcSample.SetExtendedStyle(m_lcSample.GetStyle()|LVS_EX_CHECKBOXES); m_lcSample.InsertColumn(0,"Name",LVCFMT_LEFT,150); m_lcSample.InsertColumn(1,"Age",LVCFMT_LEFT,200); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE
}
The code marked in green italics, is the part which makes the List control (CListCtrl class) display the check boxes.
Adding and Removing Items from CListCtrl with check boxes:
Adding and removing items from this modified CListCtrl normally as explained in CListCtrl usage article. The functions InsertItem and DeleteItem can be called as in a normal List control.
Retrieving the checked items from CListCtrl:
As in a normal case, one would like to retrieve items which are checked inside the CListCtrl. This is achieved by using the CListCtrl.GetCheck() function. This function returns the checked / unchecked status of the row inside the list control. Based on the return value, the programmer can decide and retrieve the items using CListCtrl.GetItemText() function.
int nItem = 0; //Represents the row number inside CListCtrl for(nItem =0 ; nItem < m_lcSample.GetItemCount(); nItem++) { BOOL bChecked = m_lcSample.GetCheck(nItem); if( bChecked == 0 ) { CString strText = m_lcSample.GetItemText(nItem, 0); MessageBox(strText); } }
Using check box will be a nice option for multiple selections as above. Care should be taken to retain the old style of the list control by using CListCtrl.GetStyle() function and combining the check box option with this.
The CListCtrl.GetCheck(nItem) function takes the Zero based index of the row numbers. This function implements the behavior of the win32 macro ListView_GetCheckState.