Collapse Event (TreeView Control) Example

This example adds one Node object, with several child nodes, to a TreeView control. When the user collapses a Node, the code checks to see how many children the Node has. If it has more than one child, the Node is re-expanded. To try the example, place a TreeView control on a form and paste the code into the form's Declarations section. Run the example, and double-click a Node to collapse it and generate the event.

Private Sub Form_Load()
   TreeView1.Style = tvwTreelinesPlusMinusText ' Style 6.
   Dim nodX As Node
   Set nodX = TreeView1.Nodes.Add(,,"DV","Da Vinci")
   Set nodX = TreeView1.Nodes.Add("DV",tvwChild,"T","Titian")
   Set nodX = TreeView1.Nodes.Add("T",tvwChild,"R","Rembrandt")
   Set nodX = TreeView1.Nodes.Add("R",tvwChild,,"Goya")
   Set nodX = TreeView1.Nodes.Add("R",tvwChild,,"David")
   nodX.EnsureVisible   ' Show all nodes.
End Sub

Private Sub TreeView1_Collapse(ByVal Node As Node)   
   ' If the Node has more than one child node,
   ' keep the node expanded.
   Select Case Node.Children 
      Case Is > 1
         Node.Expanded = True
   End Select
End Sub