Solved

CListCtrl with CheckBox option in Report View

Posted on 2000-03-08
6
854 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now