Solved

CListCtrl with CheckBox option in Report View

Posted on 2000-03-08
6
872 Views
Last Modified: 2007-10-18
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
Comment
Question by:jaiken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 4

Expert Comment

by:abancroft
ID: 2597117
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
 

Author Comment

by:jaiken
ID: 2597256
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
 
LVL 4

Expert Comment

by:abancroft
ID: 2597407
Could you post your hit test code?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jaiken
ID: 2597440
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
 
LVL 4

Accepted Solution

by:
abancroft earned 50 total points
ID: 2597496
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
 

Author Comment

by:jaiken
ID: 2597623
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question