Visual Basic Common Control API Routines
SendMessage: Add ListView Check Boxes
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
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.

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.

 BAS Module Code

 Form Code
Add a check button to the project (Check1) with the caption "Check Boxes", and set its initial value to 0. In addition, add a command button (Command1).

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 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
   state = Check1.Value = 1
  'set the new listview style
   Call SendMessage(ListView1.hwnd, _
                    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
   If Len(b) Then MsgBox b

End Sub
Run your project, and populate your ListView as usual. With the Check Box box checked (that's a mouthful), along with the standard items listview text each line will be preceded by checkbox.

Select (check) several items (note that selecting a list item doesn't check the box; the checkbox itself must be hit), and 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.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter