Prerequisites |
This method is intended for Visual Basic 5 or Visual Basic
6 where the Common Control library used is the MSComCtl 5 version (comctl32.ocx). Because the VB6-specific mscomctl.ocx (Common Controls 6)
is a complete implementation of comctl32.dll and not reliant on the version of comctl32.dll installed, this routine may not work when applied
to a listview created from the VB6-specific mscomctl.ocx.
Enhanced Comctl32 functionality is only available to users with
comctl32.dll version 4.70 or greater installed. This dll is typically installed with IE3.x or greater. |
|
By
setting a ListView style bit using the Windows API SendMessage with the message LVS_EX_FULLROWSELECT, selecting an item will cause the
selection rectangle to highlight and span the entire row. Removing this bit restores the default selection style.
This example does not contain all code required to construct
the illustration shown. The routine provided here is designed to be applied to an existing project utilizing a ListView control with
subitems. |
|
BAS
Module Code |
|
Place the following code into the general declarations
area of a bas module: |
|
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Public Const LVM_FIRST = &H1000
Public Const LVM_SETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 54)
Public Const LVM_GETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 55)
Public Const LVS_EX_FULLROWSELECT = &H20
|
|
Form
Code |
|
Add a check button to the project (Check1) with the caption
"Use Full Row Select". Set its initial value to 0. Add the following code to the check button sub: |
|
Option Explicit
Private Sub Check1_Click()
Dim state As Long
'state will be true when the checkbox
'style is 1 (checked) or False when
'unchecked
state = Check1.Value = 1
'set the new listview style
Call SendMessage(ListView1.hwnd, _
LVM_SETEXTENDEDLISTVIEWSTYLE, _
LVS_EX_FULLROWSELECT, ByVal state)
End Sub
|
|
Comments |
Run your project, and populate your ListView as usual.
Select an item; it will exhibit the normal highlighting - only the first item is selected. Click the Check1 button, and reselect an item. The
selection will now span the entire row. Unchecking Check1 will revert to the default selection style. |
|