This
page demonstrates adding the ability to have checkmarks to the left of the item icon in report view. Under Win95 the check boxes are not not
pretty.
By setting a ListView extended style bit using the API SendMessage with the message LVS_EX_CHECKBOXES, the ListView rows will have a
left-most checkbox. 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 provided here is designed to be applied to
the project constructed through the ListView Demo, or to an existing project utilizing a ListView control with subitems.
This example does not contain all code required to construct
the illustration shown. The routine provided 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 LVS_EX_FULLROWSELECT As Long = &H20
Private Const LVS_EX_GRIDLINES As Long = &H1
Private Const LVS_EX_CHECKBOXES As Long = &H4
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
'Add the following code to the Command1 button:
Private Sub Command1_Click()
Dim i As Long
Dim r As Long
Dim LV As LV_ITEM
'a string to build the msgbox text with
Dim b As String
'the initial msgbox text...
b = "The following ListView items are checked (0-based):" & vbCrLf & vbCrLf
'iterate through each item, checking its item state
For i = 0 To ListView1.ListItems.Count - 1
r = SendMessage(ListView1.hwnd, LVM_GETITEMSTATE, i, ByVal LVIS_STATEIMAGEMASK)
'when an item is checked, the LVM_GETITEMSTATE call
'returns 8192 (&H2000&).
If (r And &H2000&) Then
'it is checked, so pad the LV_ITEM string members
With LV
.cchTextMax = MAX_PATH
.pszText = Space$(MAX_PATH)
End With
'and retrieve the value (text) of the checked item
Call SendMessage(ListView1.hwnd, LVM_GETITEMTEXT, i, LV)
'continue building the msgbox string with the info
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
|