CListCtrl with Checkboxes

   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:

  1. This article assumes that an MFC application is already created with a dialog box and a list control.
  2. The Resource Identifier for the List control is assumed to be IDC_LISTCONTROLSAMPLE.
  3. 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.
  4. 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.

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > c > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction