TreeView Control (VBCCRTreeView)
The VBCCRTreeView 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(TvwNodes): Collection of all nodes in TreeViewSelectedItem(TvwNode): Returns currently selected nodePathSeparator(String): Character used to separate node levels in pathsIndentation(Single): Sets the indentation distance for child nodesLineStyle(TvwLineStyleConstants): Controls the appearance of connecting linesCheckBoxes(Boolean): Enables/disables checkboxes for nodesSingleSel(Boolean): Enables/disables single selection modeHideSelection(Boolean): Controls selection visibility when control loses focusLabelEdit(TvwLabelEditConstants): Label editing modeScroll(Boolean): Whether to allow scrollingEnabled(Boolean): Whether control is enabledFont(StdFont): Font properties
TvwNode Properties
Key(String): Unique identifier for the nodeText(String): Display textTag(String): Additional data associated with the nodeIndex(Long): Position in the Nodes collectionImage(Variant): Image index or keySelectedImage(Variant): Selected image index or keyExpanded(Boolean): Whether the node is expandedChildren(Long): Number of child nodes (read-only)Child(TvwNode): First child node (read-only)Root(TvwNode): Root node (read-only)Parent(TvwNode): Parent node (read-only)Previous(TvwNode): Previous sibling node (read-only)Next(TvwNode): Next sibling node (read-only)FirstSibling(TvwNode): First sibling node (read-only)LastSibling(TvwNode): Last sibling node (read-only)Checked(Boolean): Checkbox state (when CheckBoxes is enabled)
Node Relationship Constants (TvwNodeRelationshipConstants)
Used for Nodes.Add and TvwNode.Move methods' Relationship parameter:
TvwNodeRelationshipFirst(0) - As first sibling nodeTvwNodeRelationshipLast(1) - As last sibling nodeTvwNodeRelationshipNext(2) - As next sibling nodeTvwNodeRelationshipPrevious(3) - As previous sibling nodeTvwNodeRelationshipChild(4) - Add as child node
Methods
Control Methods
Refresh(): Refreshes the displayHitTest(X As Single, Y As Single) As TvwNode: Tests if a point is over a nodeHitTestInsertMark(X As Single, Y As Single, [After As Boolean]) As Long: Tests insert mark positionGetVisibleCount() As Long: Gets the number of visible nodesStartLabelEdit(): Starts label editing for the selected nodeEndLabelEdit([Discard As Boolean]): Ends label editingResetForeColors(): Resets all node foreground colorsDrag([Action]): Drag and drop operationSetFocus(): Sets focusOLEDrag(): OLE drag and dropZOrder([Position]): Sets Z order
TvwNodes Collection Methods
Add([Relative As Variant], [Relationship As TvwNodeRelationshipConstants], [Key As String], [Text As String], [Image As Variant], [SelectedImage As Variant]) As TvwNode: Adds a new nodeRemove(Index): Removes a nodeClear(): Clears all nodesExists(Index) As Boolean: Checks if a node existsItem(Index) As TvwNode: Gets a node
TvwNode Methods
EnsureVisible(): Ensures the node is visible by expanding parent nodes if necessaryCreateDragImage() As IPictureDisp: Creates a drag image for the nodeMove([Relative As Variant], [Relationship As TvwNodeRelationshipConstants]): Moves the node to a new locationSelectedIndex() As Long: Gets the selected index
Events
Click(): Click eventDblClick(): Double-click eventNodeClick(Node As TvwNode, Button As Integer): Fires when a node is clickedNodeDblClick(Node As TvwNode, Button As Integer): Fires when a node is double-clickedNodeCheck(Node As TvwNode): Fires when a node's checkbox state changesNodeBeforeCheck(Node As TvwNode, ByRef Cancel As Boolean): Fires before a node's checkbox changesNodeSelect(Node As TvwNode): Fires when a node is selectedNodeBeforeSelect(Node As TvwNode, ByRef Cancel As Boolean): Fires before a node is selectedNodeRangeSelect(Node As TvwNode, ByRef Cancel As Boolean): Fires during range selectionNodeDrag(Node As TvwNode, Button As Integer): Fires when a node is being draggedBeforeExpand(Node As TvwNode, ByRef Cancel As Boolean): Fires before a node expandsExpand(Node As TvwNode): Fires after a node expandsBeforeCollapse(Node As TvwNode, ByRef Cancel As Boolean): Fires before a node collapsesCollapse(Node As TvwNode): Fires after a node collapsesBeforeLabelEdit(ByRef Cancel As Boolean): Fires before label editing beginsAfterLabelEdit(ByRef Cancel As Boolean, NewString As String): Fires after label editing endsKeyDown(KeyCode As Integer, Shift As Integer): Key downKeyPress(KeyChar As Integer): Key pressKeyUp(KeyCode As Integer, Shift As Integer): Key upMouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single): Mouse downMouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single): Mouse moveMouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single): Mouse up
Code Examples
Basic Usage
vb
Private Sub Form_Load()
' Add root nodes
With TreeView1.Nodes
Dim rootNode As TvwNode
Set rootNode = .Add(, , "root", "Root Node")
' Add child nodes
.Add rootNode, TvwNodeRelationshipChild, "child1", "Child Node 1"
.Add rootNode, TvwNodeRelationshipChild, "child2", "Child Node 2"
' Add a grandchild
.Add TreeView1.Nodes("child1"), TvwNodeRelationshipChild, "grandchild1", "Grandchild Node 1"
End With
' Expand root node
TreeView1.Nodes("root").Expanded = True
End SubHandling Events
vb
Private Sub TreeView1_NodeClick(ByVal Node As TvwNode, ByVal Button As Integer)
Debug.Print "Selected Node: " & Node.Text
Debug.Print "Node Key: " & Node.Key
End Sub
Private Sub TreeView1_BeforeExpand(ByVal Node As TvwNode, ByRef Cancel As Boolean)
' Dynamically load child nodes
If Node.Children = 0 Then
LoadChildNodes Node
End If
End SubAdding Nodes with Images
vb
Private Sub AddNodesWithImages()
With TreeView1.Nodes
Dim root As TvwNode
Set root = .Add(, , "root1", "Documents", 1, 2)
Dim child1 As TvwNode
Set child1 = .Add(root, TvwNodeRelationshipChild, "doc1", "Word.doc", 3, 4)
Dim child2 As TvwNode
Set child2 = .Add(root, TvwNodeRelationshipChild, "doc2", "Excel.xls", 5, 6)
End With
End SubFinding a Node
vb
Private Sub FindNodeDemo()
Dim node As TvwNode
Set node = FindNodeByKey("child1")
If Not node Is Nothing Then
node.Selected = True
node.EnsureVisible
End If
End Sub
Private Function FindNodeByKey(Key As String) As TvwNode
Dim i As Long
For i = 1 To TreeView1.Nodes.Count
If TreeView1.Nodes(i).Key = Key Then
Set FindNodeByKey = TreeView1.Nodes(i)
Exit Function
End If
Next i
Set FindNodeByKey = Nothing
End FunctionRemoving a Node
vb
Private Sub RemoveNodeDemo(Key As String)
TreeView1.Nodes.Remove Key
End Sub
Private Sub RemoveSelectedNode()
If Not TreeView1.SelectedItem Is Nothing Then
TreeView1.Nodes.Remove TreeView1.SelectedItem.Index
End If
End SubLabel Editing
vb
Private Sub TreeView1_BeforeLabelEdit(ByRef Cancel As Boolean)
' Prevent editing the root node
If TreeView1.SelectedItem.Key = "root" Then
Cancel = True
MsgBox "Cannot edit the root node"
End If
End Sub
Private Sub TreeView1_AfterLabelEdit(ByRef Cancel As Boolean, ByVal NewString As String)
' Validate the new label
If Len(NewString) = 0 Then
Cancel = True
MsgBox "Label cannot be empty"
End If
End SubHit Testing
vb
Private Sub TreeView1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim node As TvwNode
Set node = TreeView1.HitTest(X, Y)
If Not node Is Nothing Then
MsgBox "Clicked on: " & node.Text
End If
End SubCheckboxes
vb
Private Sub TreeView1_NodeCheck(ByVal Node As TvwNode)
Debug.Print "Node " & Node.Text & " checked state: " & Node.Checked
End Sub
Private Sub SelectAllChildren(ByVal ParentNode As TvwNode)
Dim child As TvwNode
Set child = ParentNode.Child
Do While Not child Is Nothing
child.Checked = True
SelectAllChildren child
Set child = child.Next
Loop
End SubExpanding/Collapsing All
vb
Private Sub ExpandAll()
Dim i As Long
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes(i).Expanded = True
Next i
End Sub
Private Sub CollapseAll()
Dim i As Long
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes(i).Expanded = False
Next i
End SubGetting the Full Path
vb
Private Function GetNodePath(ByVal Node As TvwNode) As String
Dim path As String
path = Node.Text
Dim current As TvwNode
Set current = Node.Parent
Do While Not current Is Nothing
path = current.Text & TreeView1.PathSeparator & path
Set current = current.Parent
Loop
GetNodePath = path
End FunctionMoving a Node
vb
Private Sub MoveNodeDemo()
Dim sourceNode As TvwNode
Dim targetParent As TvwNode
If Not TreeView1.SelectedItem Is Nothing Then
Set sourceNode = TreeView1.SelectedItem
' Move to root level
sourceNode.Move , TvwNodeRelationshipFirst
' Or move to another parent
' sourceNode.Move targetParent, TvwNodeRelationshipChild
End If
End SubCommon Use Cases
File System Browser
vb
Private Sub LoadFileSystem()
Dim rootPath As String
rootPath = "C:\"
' Add drive nodes
With TreeView1.Nodes
.Add , , "C", "C:\", 1, 2
.Add , , "D", "D:\", 3, 4
End With
TreeView1.CheckBoxes = True
End SubOrganization Chart
vb
Private Sub CreateOrgChart()
With TreeView1.Nodes
' CEO
Dim ceo As TvwNode
Set ceo = .Add(, , "CEO", "John Doe", 1, 2)
' Managers
Dim mgr1 As TvwNode
Set mgr1 = .Add(ceo, TvwNodeRelationshipChild, "MGR1", "Alice Smith", 3, 4)
Dim mgr2 As TvwNode
Set mgr2 = .Add(ceo, TvwNodeRelationshipChild, "MGR2", "Bob Johnson", 3, 4)
' Employees under Manager 1
.Add mgr1, TvwNodeRelationshipChild, "EMP1", "Carol White"
.Add mgr1, TvwNodeRelationshipChild, "EMP2", "David Brown"
End With
ceo.Expanded = True
End SubTips
- Keys: Always use keys to identify nodes programmatically instead of relying on indices
- Images: Set up ImageList controls for different node states (normal, selected, expanded)
- EnsureVisible: Call
EnsureVisibleon a node to make sure it's visible to the user - Dynamic Loading: Use
BeforeExpandevent to dynamically load child nodes for better performance - CheckBoxes: Enable
CheckBoxesproperty to allow multi-selection via checkboxes - PathSeparator: Customize
PathSeparatorif you need specific path formats