ðHgeocities.com/Heartland/Pond/4805/checkboxes_listview.htmgeocities.com/Heartland/Pond/4805/checkboxes_listview.htm.delayedxÙPÔJÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÈà–ðâ"OKtext/html€çhâ"ÿÿÿÿb‰.HSun, 20 Jan 2002 13:04:31 GMTmMozilla/4.5 (compatible; HTTrack 3.0x; Windows 98)en, *ÙPÔJâ" How to place checkboxes in a Listview

How to place checkboxes in a Listview

This tip demonstrates how to add check boxes into the ListView control.

Declarations

Add the following code to the declarations section of a module.

Option Explicit
 
Public Declare Function SendMessageLong _
Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
 
Public Declare Function SendMessageAny _
Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
 
Public Const MAX_PATH = 260
Public Const LVM_FIRST As Long = &H1000
Public Const LVM_SETEXTENDEDLISTVIEWSTYLE _
As Long = (LVM_FIRST + 54)
Public Const LVM_GETEXTENDEDLISTVIEWSTYLE _
As Long = (LVM_FIRST + 55)
Public Const LVS_EX_FULLROWSELECT As Long = &H20
Public Const LVS_EX_GRIDLINES As Long = &H1
Public Const LVS_EX_CHECKBOXES As Long = &H4
Public Const LVM_GETITEMSTATE As Long = (LVM_FIRST + 44)
Public Const LVM_GETITEMTEXT As Long = (LVM_FIRST + 45)
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 the following code to Form1

Private Sub Form_Load()
'puts all fonts in listview1
For Ifor& = 0 To Screen.FontCount - 1
    Set itmX = ListView1.ListItems.Add(, , CStr(Screen.Fonts(Ifor&)))
Next Ifor&
'addes the checkboxes to listview1
rStyle = rStyle Or LVS_EX_CHECKBOXES
r& = SendMessageLong(ListView1.hwnd, _
LVM_SETEXTENDEDLISTVIEWSTYLE, 0&, rStyle)
End Sub
 
Private Sub Command1_Click()
Dim LI As LV_ITEM
Combo1.Clear
'looks for checked items
For i& = 0 To ListView1.ListItems.Count - 1
   r& = SendMessageLong(ListView1.hwnd, _
   LVM_GETITEMSTATE, i&, LVIS_STATEIMAGEMASK)
   'if r returns 8192 means that the checkbox is checked
    If r& = 8192 Then
      LI.cchTextMax = MAX_PATH
      LI.pszText = Space$(MAX_PATH)
      Call SendMessageAny(ListView1.hwnd, _
      LVM_GETITEMTEXT, i&, LI)
      'puts the text of the checked listitem in the combo
      Combo1.AddItem LI.pszText
   End If
Next i&
'shows the first item of the combo
If Combo1.ListCount > 0 Then Combo1.Text = _
Combo1.List(0)
End Sub

Run the app. (F5) you´ll see all fonts in the Listview with checkboxes, check some items and click on Commandbutton1, then take a look at combo1