|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Common Control API
Routines SendMessage: ListView Select / Deselect All |
||
Posted: | Saturday March 1, 1997 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Developed with: | VB4-32, Windows 95 | |
OS restrictions: | None | |
Author: | VBnet - Randy Birch | |
Prerequisites |
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. The API method detailed here
works against both the comctl32.ocx supplied with VB4-32 and 5 as well as the VB6-specific mscomctl.ocx.
ListView control with MultiSelect = True, and HideSelection = False |
|
Using
conventional methods, selecting and deselecting all items in a ListView typically involves iterating through the ListItems collection to set
each item's selected property. By utilizing the SendMessage API with LVIS_SELECTED, this monotony can be reduced to a single call.
Note that this method is for setting the ListItem highlight when selecting or deselecting. If a corresponding checking and unchecking of items is required in a control displaying ListItem checkboxes, use the method from SendMessage: Using ListView API Check Boxes 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_SETITEMSTATE = (LVM_FIRST + 43) Public Const LVM_GETITEMSTATE As Long = (LVM_FIRST + 44) Public Const LVM_GETSELECTEDCOUNT = (LVM_FIRST + 50) Public Const LVIS_SELECTED = &H2 Public Const LVIF_STATE = &H8 Public Const LVIS_STATEIMAGEMASK As Long = &HF000 Public Type LV_ITEM mask As Long iItem As Long iSubItem As Long state As Long stateMask As Long pszText As String cchTextMax As Long iImage As Long lParam As Long iIndent As Long End Type |
Form Code |
Add two command buttons to the project (Command1 and Command2), along with a label (Label1), then add the following code to the form: |
|
Option Explicit Private Sub Command1_Click() Dim LV As LV_ITEM Dim numSelected As Long With LV .mask = LVIF_STATE .state = True .stateMask = LVIS_SELECTED End With 'by setting wParam to -1, the call affects all 'listitems. To just change a particular item, 'pass its index as wParam. Call SendMessage(ListView1.hwnd, _ LVM_SETITEMSTATE, _ -1, _ LV) 'update the label numSelected = SendMessage(ListView1.hwnd, _ LVM_GETSELECTEDCOUNT, _ 0&, _ ByVal 0&) Label1.Caption = numSelected & " items are selected." End Sub Private Sub Command2_Click() Dim LV As LV_ITEM Dim numSelected As Long With LV .mask = LVIF_STATE .state = False .stateMask = LVIS_SELECTED End With 'by setting wParam to -1, the call affects all 'listitems. To just change a particular item, 'pass its index as wParam. Call SendMessage(ListView1.hwnd, _ LVM_SETITEMSTATE, _ -1, _ LV) 'update the label numSelected = SendMessage(ListView1.hwnd, _ LVM_GETSELECTEDCOUNT, _ 0&, _ ByVal 0&) Label1.Caption = numSelected & " items are selected." End Sub |
Comments |
Run the project and populate the ListView as usual. Clicking Command1 will select everything, and Command2 will clear everything. The number of items selected is reflected in the label. |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |