TreeView Disable Node - March 2001

I am using the standard Microsoft TreeView control in an application to control the application's navigation. The user clicks on a node and jumps to a different place in the application. It works much like clicking on a tab. A problem: TreeView nodes don't have a 'disabled' property. I needed to prevent the user from jumping to some portions of the application until a prior step is completed. I posed a question in a DevX news group and got some helpful information from Michael Culley on how to use subclassing to disable a node. I created a 'demo' program to test the theory. It works like a charm. So in collaboration with Michael, I enhanced the demo and made it the March 2001 tip-of-the-month. 

Demo Program

I wrote a VB 6 demo program to show how to disable a node. The demo uses the VB 6 TreeView control [Microsoft Windows Common Controls 6.0 (SP4)]. You could substitute the version 5 control. A screen shot of the demo:

Click on a 'disabled node' and notice that nothing happens. The forms caption contains information about the node you clicked on. Click the 'Disable...' button to disable the node and any children. Click the 'Enable...' button to enable all nodes.

Class clsTVWDisableNode

I encapsulated the code to disable TreeView nodes in a class module called 'clsTVWDisableNode'. An object view of the class.

Using the Code

To use the code you need to add Michael Culley's subclassing control to a form. It is provided as part of the demo. You can also get it [with source code] by clicking here [60K]. Michael's web site is: He developed the control entirely on his own - with help from the DevX community. You will need to register the control. The object model: 

Object Browser

Sample Calling Code

' Implementation, put in a form's declarations section
Dim WithEvents mobjTVWDisableNode As clsTVWDisableNode
' Setup
Private Sub SetupSubclassTreeView()
    ' Set mobjTVWDisableNode properties
    Set mobjTVWDisableNode = New clsTVWDisableNode
    With mobjTVWDisableNode
        .lEnabledColor = vbWindowText 'default, is set in Class_Initialize
        .SubclassStart TreeView1, mcFreeSubClass1 'start subclassing
    End With
End Sub
' Termination
Private Sub Form_Unload(Cancel As Integer)
    With mobjTVWDisableNode 'stop subclassing
    End With
    Set mobjTVWDisableNode = Nothing
End Sub
' Callback Message Processing
Private Sub mobjTVWDisableNode_MessageKilled(lMsg As Long, sText As String)
    ' WithEvents causes this event to be fired from clsTVWDisableNode
    ' Show the node's text and the message number in the form's caption.
    Caption = "Node " & sText & " Click Ignored [msg " & lMsg & "]"
End Sub

Include In Your Application

VBDotComSubClassLib Use Michael Culley's User Control
to provide subclassing support. 
Add it to one of your forms.
clsTVWDisableNode.cls Provides properties and methods needed
to disable a node.

The Demo Program Uses modConnectToWebSite

The demo program uses modConnectToWebSite.bas to display this page. For details on the use of that module click here.

Download VB 6 Source Code

Click here to download the VB 6 source code [30k]

Previous Tip   Tip Index    Next Tip


Updated 2004/03/06