Solved

CListCtrl with CheckBox option in Report View

Posted on 2000-03-08
6
864 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 116
How to convert MFC APP to Win32 APP. 19 97
Unresolved External Symbols 3 109
How to convert Structure to vector of byte and vice versa 5 24
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

829 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