Visual Basic Common Control API Routines
SendMessage: Toggling TreeView Check Box Visibility
Posted:   Friday September 17, 1999
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6 using comctl32.ocx
Developed with:   VB5, Windows 98
OS restrictions:   None
Author:   Brad Martinez
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.

Visual Basic 6 users have extended functionality built-in to the newer mscomctl32.ocx, which does not rely on the Windows comctl32.dll. This code is targeted towards users opting for the older comctl32.ocx using comctl32.dll.

So now that the ability to display checkboxes has been added to the application (SetWindowLong: Add TreeView Check Boxes via API), there may be times when it is desirable to hide this functionality. Simply resetting the TVS_CHECKBOXES flag does not do this, as the state image icons are still attached to the treeview.

Brad Martinez demonstrates here how to "remove" the checkboxes, and the requisite destruction of the associated imagelist that was created to display the checkboxes.

This code can be added to a new or existing project. The demo pictured here is created from the above link; this only adds the additional display/hide functionality. 

 BAS Module Code
This code duplicates some of the API and constant declarations found in SetWindowLong: Add TreeView Check Boxes via API ; it is unnecessary if these are already in your project.

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.
'Brad Martinez,

Public Const TVS_CHECKBOXES As Long = &H100  '>= IE3
Public Const TV_FIRST As Long = &H1100
Public Const TVIF_STATE As Long = &H8
Public Const TVIS_STATEIMAGEMASK As Long = &HF000
Public Const TVSIL_NORMAL As Long = 0
Public Const TVSIL_STATE As Long = 2

Public Declare Function ImageList_Destroy Lib "comctl32" _
    (ByVal hIml As Long) As Boolean

Public Const GWL_STYLE As Long = (-16)

Public Declare Function GetWindowLong Lib "user32" _
     Alias "GetWindowLongA" _
    (ByVal hWnd As Long, _
     ByVal nIndex As Long) As Long
 Form Code
To the form containing the treeview (Treeview1), add a command button (Command1) and the following code:

Option Explicit

Private hwndTV As Long

Private Sub Form_Load()

   hwndTV = TreeView1.hWnd
End Sub

Private Function SetTVStyle(hwndTV As Long) As Boolean

   Dim dwStyle As Long
   dwStyle = GetWindowLong(hwndTV, GWL_STYLE)
   If dwStyle And TVS_CHECKBOXES Then
     'destroy the imagelist 
      Call TVRemoveStateImagelist(hwndTV)

     'remove the check state bit  
      SetTVStyle = CBool(SetWindowLong(hwndTV, _
                                       GWL_STYLE, _
                                       dwStyle Xor TVS_CHECKBOXES))
     'add the check state bit.  The control will 
     'create the required state imagelist
      SetTVStyle = CBool(SetWindowLong(hwndTV, _
                                       GWL_STYLE, _
                                       dwStyle Or TVS_CHECKBOXES))
   End If
End Function

Private Sub Command1_Click()

  'toggle the display of the checkboxes
   Call SetTVStyle(hwndTV)
End Sub

Private Function TVRemoveStateImagelist(hwndTV As Long) As Boolean

   Dim hIml As Long

  'Setting TVS_CHECKBOXES creates a state imagelist,
  'removing this bit but doesn't remove and 
  'destroy the imagelist from the treeview.
  'This requires a call to ImageList_Destroy
   hIml = SendMessage(hwndTV, _
                      TVM_SETIMAGELIST, _
                      TVSIL_STATE, _
                      ByVal 0&)
   If hIml Then TVRemoveStateImagelist = ImageList_Destroy(hIml)

End Function
Pressing the Toggle button will display or hide the checkboxes.

Note however that hiding the checks also destroys the imagelist, and that subsequent displaying of the checkboxes will cause all to appear unchecked again.  If it is necessary for your application to maintain the checked status of individual items when removing the checkboxes, your app is responsible for tracking those items selected, then reinstating the checks on the subsequent showings.


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