• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 920
  • Last Modified:

CListCtrl with CheckBox option in Report View

I have a list control with the checkbox option on.  I wanted to set it up so no matter where on the row a user clicked the checkbox would work.  I followed code on codeguru and on my click event I check if the click is inside of any of the rows (inside GetItemRect with LVIR_BOUNDS) and if it is I call SetCheck on that row.  It all works except for if I try to click inside of the checkbox.  Now it gets to the OnItemChangedList twice.  ie - it automatically is checked as that is handled when you check and it also is handled a second time through my click code.  Therefore it does a quick check/uncheck combo.  I looked up how to calculate bounding rectanges on the list view so I could return from the click event if the user clicked in the checkbox and there is LVIR_BOUNDS, LVIR_ICON, and LVIR_LABEL.  I didn't see anything for getting the rectangle of the checkbox.  Anyone have any good ideas on this one.
0
jaiken
Asked:
jaiken
  • 3
  • 3
1 Solution
 
abancroftCommented:
I think the supplied check boxes are just state icons. If so, you could hit test the list control to see if the point is on the state icon.

The message you need to send is LVM_HITTEST (CListCtrl::HitTest() in MFC).
0
 
jaikenAuthor Commented:
I tried your suggestion.  It works for the checkbox but breaks the rest of column 0 (which contains a color icon).  Now I can click on the checkbox or on any column other than column 0 and it works but if I click on column 0 outside of the checkbox (like on the icon) it does not work. Before the fix I could click on column 0 or any other column except for the checkbox inside of Column 0.
0
 
abancroftCommented:
Could you post your hit test code?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jaikenAuthor Commented:
Below are the 2 functions that are called that end up not working correctly.



void CWIT_TAKE1Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
      NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
      *pResult = 0;

      if (pNMListView->uOldState == 0 && pNMListView->uNewState == 0)
            return;      // No change

      BOOL bPrevState = (BOOL)(((pNMListView->uOldState & 
                        LVIS_STATEIMAGEMASK)>>12)-1);   // Old check box state
      if (bPrevState < 0)      // On startup there's no previous state
            bPrevState = 0; // so assign as false (unchecked)

      // New check box state
      BOOL bChecked=(BOOL)(((pNMListView->uNewState & LVIS_STATEIMAGEMASK)>>12)-1);  
      if (bChecked < 0) // On non-checkbox notifications assume false
            bChecked = 0;

      if (bPrevState == bChecked) // No change in check box
            return;
      
      if (bChecked)
      {
            if (m_SpreadsheetInformation[pNMListView->iItem].CurrentIcon != AfxGetApp()->LoadIcon(IDI_WHITE))
                  return;
            if (m_NumChecked < 10)
            {
                  int x;
                  m_NumChecked++;
                  for (x = 0; x < 10; x++)
                  {
                        if (!GIconArray[x].bUsed)
                              break;
                  }
                  GIconArray[x].iSpot = pNMListView->iItem;
                  m_imageList.Replace(pNMListView->iItem, GIconArray[x].hIcon);
                  m_SpreadsheetInformation[pNMListView->iItem].CurrentIcon = GIconArray[x].hIcon;
                  GIconArray[x].bUsed = true;
            }
            else
            {
                  MessageBox("The WIT program only allows the graphing of 10 selected items from the spreadsheet.","Spreadsheet Error",MB_ICONWARNING|MB_OK);
                  m_List.SetCheck(pNMListView->iItem, false);
            }
      }
      else
      {
            for (int x = 0; x < 10; x++)
            {
                  if (GIconArray[x].iSpot == pNMListView->iItem)
                  {

                        m_NumChecked--;
                        m_SpreadsheetInformation[pNMListView->iItem].CurrentIcon = AfxGetApp()->LoadIcon(IDI_WHITE);
                        m_imageList.Replace(pNMListView->iItem, AfxGetApp()->LoadIcon(IDI_WHITE));
                        GIconArray[x].bUsed = false;
                        break;
                  }
            }
      }
}





void CWIT_TAKE1Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{

            // Get the current mouse location and convert it to client
       // coordinates.
       DWORD pos = GetMessagePos();
       CPoint pt(LOWORD(pos), HIWORD(pos));
       m_List.ScreenToClient(&pt);
      
//HERE IS  YOUR CODE.....      
         UINT flag = LVHT_ONITEMSTATEICON;
         UINT *pFlag = &flag;
         int ret = m_List.HitTest(pt,pFlag);
         if (ret != -1)
         {
                  UINT flag2 = LVIS_SELECTED | LVIS_FOCUSED;
                  m_List.SetItemState(ret, 0, flag2);
                  return;
         }

       // Get indexes of the first and last visible items in listview
       // control.
       int index = m_List.GetTopIndex();
       int last_visible_index = index + m_List.GetCountPerPage();
       if (last_visible_index > m_List.GetItemCount())
           last_visible_index = m_List.GetItemCount();

       // Loop until number visible items has been reached.
       while (index <= last_visible_index)
       {
           // Get the bounding rectangle of an item. If the mouse
           // location is within the bounding rectangle of the item,
           // you know you have found the item that was being clicked.
           CRect r;
           m_List.GetItemRect(index, &r, LVIR_BOUNDS);

           //CAN WE CHECK IF THE USER CLICKED INSIDE OF THE CHECKBOX
               //AND IF SO JUST RETURN
               
               if (r.PtInRect(pt))
           {
                     UINT flag = LVIS_SELECTED | LVIS_FOCUSED;
               m_List.SetItemState(index, 0, flag);
               m_List.SetCheck(index, !(m_List.GetCheck(index)));
               break;
           }

           // Get the next item in listview control.
           index++;
       }
      *pResult = 0;

}
0
 
abancroftCommented:
I think you want to use code this as follows:

void CWIT_TAKE1Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{

  DWORD pos = GetMessagePos();
  CPoint pt(LOWORD(pos), HIWORD(pos));
 
  m_List.ScreenToClient(&pt);

  UINT uFlags;
  int nItem = m_List.HitTest(pt, &uFlags);

  // They hit an actual item....
  if (nItem!=-1)
  {
    m_List.SetItemState(nItem, 0, LVIS_SELECTED | LVIS_FOCUSED);

    // If they didn't hit the check box...
    if (!(uFlags & LVHT_ONSTATEICON))
    {
      m_List.SetCheck(nItem, !(m_List.GetCheck(nItem)));
    }
  }
}

0
 
jaikenAuthor Commented:
OK I took your code and added an else /*if nItem == -1*/ and put the rest of my code in for the selection outside of column 0 and it all seemed to work.  Your code allowed me to select anywhere in column 0 and mine worked for the other columns.  Thank you so much for your help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now