Skip to content

TreeView Control (VBComCtlTreeView)

The VBComCtlTreeView control provides an enhanced tree view interface for displaying hierarchical data structures. It offers improved functionality over the standard MSComCtl TreeView control with better performance and visual features.

Properties

Key Properties

  • Nodes: Collection of all nodes in the TreeView
  • SelectedItem: Returns the currently selected node
  • PathSeparator: Character used to separate node levels
  • Indentation: Sets the indentation for child nodes
  • LineStyle: Controls the appearance of connecting lines
  • CheckBoxes: Enables/disables checkboxes for nodes
  • SingleSel: Enables/disables single selection mode
  • HideSelection: Controls selection visibility when control loses focus

Methods

Essential Methods

  • Add([Relative As Node], [Relationship As TreeRelationshipConstants], [Key As String], [Text As String]): Adds a new node
  • Remove(Node As Node): Removes a specified node
  • Expand(): Expands a node to show its children
  • Collapse(): Collapses a node to hide its children
  • GetVisibleCount(): Returns the number of visible nodes
  • FindNode(FindWhat As String, [Where As TreeFindConstants]): Searches for a node

Events

  • NodeClick(ByVal Node As Node): Fires when a node is clicked
  • BeforeExpand(ByVal Node As Node, Cancel As Integer): Fires before a node expands
  • AfterExpand(ByVal Node As Node): Fires after a node expands
  • BeforeCollapse(ByVal Node As Node, Cancel As Integer): Fires before a node collapses
  • NodeCheck(ByVal Node As Node): Fires when a node's checkbox state changes

Code Examples

Basic Usage

vb
Private Sub Form_Load()
    ' Add root nodes
    With TreeView1.Nodes
        Dim rootNode As Node
        Set rootNode = .Add(, , "root", "Root Node")
        
        ' Add child nodes
        .Add rootNode, tvwChild, "child1", "Child Node 1"
        .Add rootNode, tvwChild, "child2", "Child Node 2"
        
        ' Add a child to Child Node 1
        .Add "child1", tvwChild, "grandchild1", "Grandchild Node 1"
    End With
    
    ' Expand root node
    TreeView1.Nodes("root").Expanded = True
End Sub

Handling Events

vb
Private Sub TreeView1_NodeClick(ByVal Node As Node)
    Debug.Print "Selected Node: " & Node.Text
    Debug.Print "Node Key: " & Node.Key
    Debug.Print "Node Level: " & Node.Level
End Sub

Private Sub TreeView1_BeforeExpand(ByVal Node As Node, Cancel As Integer)
    ' Example: Dynamically load child nodes
    If Node.Children = 0 Then
        LoadChildNodes Node
    End If
End Sub

Dynamic Node Loading

vb
Private Sub LoadChildNodes(ParentNode As Node)
    ' Example: Loading directory structure
    Dim FSO As New FileSystemObject
    Dim Folder As Folder
    Dim SubFolder As Folder
    
    Set Folder = FSO.GetFolder(ParentNode.Tag)
    
    For Each SubFolder In Folder.SubFolders
        With TreeView1.Nodes
            .Add ParentNode, tvwChild, SubFolder.Path, SubFolder.Name
        End With
    Next SubFolder
End Sub

Common Use Cases

File System Browser

vb
Private Sub CreateFileSystemTree()
    Dim FSO As New FileSystemObject
    Dim Drive As Drive
    
    TreeView1.Nodes.Clear
    
    For Each Drive In FSO.Drives
        If Drive.IsReady Then
            With TreeView1.Nodes
                Dim nodeKey As String
                nodeKey = "Drive_" & Drive.DriveLetter
                .Add , , nodeKey, Drive.DriveLetter & ":\"
                .Item(nodeKey).Tag = Drive.DriveLetter & ":\"
            End With
        End If
    Next Drive
End Sub

Organization Chart

vb
Private Sub CreateOrgChart()
    With TreeView1.Nodes
        Dim ceo As Node
        Set ceo = .Add(, , "CEO", "Chief Executive Officer")
        
        ' Add department heads
        .Add ceo, tvwChild, "CFO", "Chief Financial Officer"
        .Add ceo, tvwChild, "CTO", "Chief Technology Officer"
        .Add ceo, tvwChild, "COO", "Chief Operations Officer"
        
        ' Add team members
        .Add "CTO", tvwChild, "Dev1", "Development Team Lead"
        .Add "CTO", tvwChild, "Dev2", "QA Team Lead"
    End With
End Sub

Best Practices

  1. Memory Management
vb
' Clear nodes properly
Private Sub ClearTree()
    TreeView1.Nodes.Clear
    Set TreeView1.ImageList = Nothing
End Sub
  1. Performance Optimization
vb
Private Sub OptimizedNodeAddition()
    TreeView1.Visible = False
    
    ' Add nodes here
    
    TreeView1.Visible = True
End Sub
  1. Error Handling
vb
Private Sub SafeNodeOperation(NodeKey As String)
    On Error Resume Next
    Dim Node As Node
    Set Node = TreeView1.Nodes(NodeKey)
    
    If Err.Number = 0 Then
        ' Node exists, perform operations
        Node.Expanded = True
    Else
        ' Handle node not found
        Debug.Print "Node not found: " & NodeKey
    End If
    On Error GoTo 0
End Sub

Known Issues and Workarounds

  1. Recursive Node Deletion
vb
Private Sub DeleteNodeAndChildren(Node As Node)
    Dim Child As Node
    
    ' Delete children first
    While Node.Children > 0
        Set Child = Node.Child
        TreeView1.Nodes.Remove Child.Index
    Wend
    
    ' Delete the node itself
    TreeView1.Nodes.Remove Node.Index
End Sub

Additional Tips

  • Use the Tag property to store additional node information
  • Implement proper search functionality for large trees
  • Consider using images to enhance visual appearance
  • Implement proper cleanup in Form_Unload
  • Use error handling for all node operations
  • Consider implementing keyboard navigation support

VB6 and LOGO copyright of Microsoft Corporation