Visual Basic Common Control API Routines
SendMessage: Toggle ListView Check Box State
     
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
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.


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

 BAS Module Code
Place the following code into the general declarations area of a bas module:

 Form Code
Add a check button to the project (Check1) with the caption "Check Boxes", and set its initial value to 0. Add two more command buttons .. Command1 and Command2. Add the following code to the form sub:

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
 Comments
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.

 
 

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