How to highlight a whole row in the ListView common control in a multicolumn report view mode (230049)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 4.2
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ .NET (2002)
    • Microsoft Visual C++ .NET (2003)

This article was previously published under Q230049
Note Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

SUMMARY

The ListView common control in a multicolumn report view mode highlights only the first column when a row is selected. This behavior is by design.

MORE INFORMATION

To enable complete row selection, you need to send the LVM_SETEXTENDEDLISTVIEWSTYLE message to the control and specify the LVS_EX_FULLROWSELECT extended style.

Create a CListCtrl derived class, CMyListCtrl. Using ClassWizard, create a handler for the WM_CREATE message for CMyListCtrl. Then, in the definition of the handler, copy the code shown below. Now use CMyListCtrl in place of CListCtrl in your project.
int CMyListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
   if (CListCtrl::OnCreate(lpCreateStruct) == -1)
      return -1;

   DWORD dwStyle = ::SendMessage(m_hWnd,LVM_GETEXTENDEDLISTVIEWSTYLE,0,0);
   dwStyle |= LVS_EX_FULLROWSELECT;
   ::SendMessage(m_hWnd,LVM_SETEXTENDEDLISTVIEWSTYLE,0,dwStyle);

// or you could also use

// ListView_SetExtendedListViewStyle(
//            m_hWnd, 
//            ListView_GetExtendedListViewStyle(m_hWnd)
//            | LVS_EX_FULLROWSELECT);


// or you could also use

// SetExtendedStyle(GetExtendedStyle() | LVS_EX_FULLROWSELECT);

   return 0;
}

				


The code above works only for dynamically created list controls. For controls created on the dialog resource, similar code should be placed in the OnInitDialog function.

REFERENCES

  • 147842 How To Detect a Mouse Click on Any Column of List View Control

  • LISTHDR: Demonstrates the List View and Header Common Controls
  • 181440 How To Add Full Row Select Functionality to a ListView Control


    NOTE: Article KB 181440 shows how this could be achived in Visual Basic.
  • 131788 OdListVw.exe Highlights Entire Row in a ListView Control


Modification Type:MinorLast Reviewed:5/3/2005
Keywords:kbCmnCtrls kbCtrl kbhowto kbListView KB230049 kbAudDeveloper