Home  |  About  |   Search   

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

RunCommand Constants

Terms of Use


 

API: Enabling Full Row Select in a ListView

Author(s)
Dev Ashish

    The ListView control which ships with ODE doesn't have a FullRowSelect property like it's VB6 cousin.  The FullRowSelect property allows you to specify if the entire row of the selected item is highlighted and clicking anywhere on an item's row causes it to be selected.

    By using the SendMessage API function, we can send the LVM_SETEXTENDEDLISTVIEWSTYLE message, with lParam value set to True, to the ListView window which enables the FullRowSelect feature.  To revert back to the normal select mode, we call SendMessage again with LVM_SETEXTENDEDLISTVIEWSTYLE and flip the lParam value to False.

    Here are two functions to set and clear this style for a ListView control.

'*************** Code Start *****************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
'
Private Const LVM_FIRST As Long = &H1000

'When an item is selected, the item and all its subitems are highlighted.
'  Version 4.70 and later of comctl32.dll
'
'  Windows NT: Requires version 5.0 or later (or version 4.0
'                                          with Internet Explorer 3.0 and later).
'  Windows: Requires Windows 98 (or Windows 95 with _
'                                         Internet Explorer 3.0 or later).
Private Const LVS_EX_FULLROWSELECT As Long = &H20

'Sets extended styles in list view controls.
'dwExMask allows you to modify one or more extended styles without having
'to retrieve the existing styles first. For example, if you pass
'LVS_EX_FULLROWSELECT for dwExMask and 0 for dwExStyle, the
'LVS_EX_FULLROWSELECT style will be cleared, but all other
'styles will remain the same.
Private Const LVM_SETEXTENDEDLISTVIEWSTYLE As Long = LVM_FIRST + 54

'Retrieves the extended styles that are currently in use for a given list view control.
Private Const LVM_GETEXTENDEDLISTVIEWSTYLE As Long = LVM_FIRST + 55

Private Declare Function apiSendMessageLong Lib "user32" _
   Alias "SendMessageA" _
   (ByVal hwnd As Long, _
   ByVal Msg As Long, _
   ByVal wParam As Long, _
   ByVal lParam As Long) _
   As Long

Function fLVFullRowSelect(LV As Control) As Boolean
'Sets FullRowSelect for a ListView
'
Dim lngStyle As Long

   'Find out the current style first
   lngStyle = apiSendMessageLong(LV.hwnd, _
                                       LVM_GETEXTENDEDLISTVIEWSTYLE, _
                                       0&, 0&)
                                       
   If lngStyle And LVS_EX_FULLROWSELECT Then
      'Listview already has FullRowSelect enabled
      fLVFullRowSelect = True
   Else
      'Set the property to True
      fLVFullRowSelect = (apiSendMessageLong(LV.hwnd, _
                                       LVM_SETEXTENDEDLISTVIEWSTYLE, _
                                       LVS_EX_FULLROWSELECT, True) = 0)
   End If
End Function

Function fResetLVFullRowSelect(LV As Control) As Boolean
' Removes FullRowSelect for a listview
'
Dim lngStyle As Long

   'Find out the current style first
   lngStyle = apiSendMessageLong(LV.hwnd, _
                                       LVM_GETEXTENDEDLISTVIEWSTYLE, _
                                       0&, 0&)

   If lngStyle And LVS_EX_FULLROWSELECT Then
      'If the style is currently set, remove it
      fResetLVFullRowSelect = Not (apiSendMessageLong(LV.hwnd, _
                                             LVM_SETEXTENDEDLISTVIEWSTYLE, _
                                             LVS_EX_FULLROWSELECT, 0) = 0)
   Else
      'The FullRowSelect property is already removed
      fResetLVFullRowSelect = True
   End If
End Function
'*************** Code End *****************

© 1998-2009, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer