The
routines here set and retrieve checked ListItems from a ListView control in report view.
Basically, here we are again using the enhanced comctl32.dll functionality, this time using the LVIS_STATEIMAGEMASK and LVIF_STATE to toggle
the bits at &H1000 and &H2000. In addition, by using the message LVM_GETITEMSTATE against each item, its checked or unchecked state
can be determined. Finally, by using LVM_GETITEMTEXT and the LV_ITEM data type, the string representing each selected item can be retrieved.
This example does not contain all code required to construct
the illustration shown. The routine is designed to be applied to an existing project utilizing a ListView control with subitems |
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.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const MAX_PATH As Long = 260
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETEXTENDEDLISTVIEWSTYLE As Long = (LVM_FIRST + 54)
Private Const LVM_GETEXTENDEDLISTVIEWSTYLE As Long = (LVM_FIRST + 55)
Private Const LVIF_STATE = &H8
Private Const LVS_EX_CHECKBOXES As Long = &H4
Private Const LVM_SETITEMSTATE = (LVM_FIRST + 43)
Private Const LVM_GETITEMSTATE As Long = (LVM_FIRST + 44)
Private Const LVM_GETITEMTEXT As Long = (LVM_FIRST + 45)
Private Const LVIS_STATEIMAGEMASK As Long = &HF000
Private 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
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_CHECKBOXES, ByVal state)
End Sub
Private Sub Command2_Click()
Static bState
bState = Not bState
'pass the 0-based index of the item to check/uncheck (4),
'and the state as a Boolean
SetCheck ListView1.hwnd, 4, bState
End Sub
Public Sub SetCheck(ByVal hwnd As Long, ByVal lItemIndex As Long, ByVal bState As Boolean)
Dim LV As LV_ITEM
With LV
.mask = LVIF_STATE
.state = IIf(bState, &H2000, &H1000)
.stateMask = LVIS_STATEIMAGEMASK
End With
Call SendMessage(hwnd, LVM_SETITEMSTATE, lItemIndex, LV)
End Sub
Private Sub Command1_Click()
Dim i As Integer
Dim r As Long
Dim b As String
Dim LV As LV_ITEM
b = "The following ListView items are checked (0-based):" & vbCrLf & vbCrLf
For i = 0 To ListView1.ListItems.Count - 1
r = SendMessage(ListView1.hwnd, LVM_GETITEMSTATE, i, ByVal LVIS_STATEIMAGEMASK)
If r And &H2000& Then
With LV
.cchTextMax = MAX_PATH
.pszText = Space$(MAX_PATH)
End With
Call SendMessage(ListView1.hwnd, LVM_GETITEMTEXT, i, LV)
b = b & "item " & CStr(i) & _
" ( " & Left$(LV.pszText, InStr(LV.pszText, Chr$(0)) - 1) & _
" )" & vbCrLf
End If
Next
If Len(b) Then MsgBox b
End Sub
|
Run your project, and populate your ListView as usual.
With the 'Check Box' box checked, along with the standard items ListView text, each line will be preceded by checkbox.
Clicking the Set Check button will toggle the check for the fourth item (0-based).
You can also check other items (note that selecting a list item doesn't check the box; the checkbox itself must be hit), then press the 'Get
Checked' button. The 0-based index of each checked item will appear in the messagebox, along with the text associated with that item. |