EnsureVisible Method Example

This example adds many nodes to a TreeView control, and uses the EnsureVisible method to scroll and expand the tree. 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 click the form to see the TreeView expand.

Private Sub Form_Load()
   Dim nodX As Node
   Dim i as Integer
   TreeView1.BorderStyle = vbFixedSingle ' Show borders.
   
   Set nodX = TreeView1.Nodes.Add(,,,"Root")   ' Add first node.
   For i = 1 to 15   ' Add 15 nodes
      Set nodX = TreeView1.Nodes.Add(i,,,"Node " & CStr(i))
   Next i

   Set nodX = TreeView1.Nodes.Add(,,,"Bottom")   ' Add one with text.
   Set nodX = TreeView1.Nodes.Add(i,,,"Expanded") ' Add child to node.
   Set nodX = TreeView1.Nodes.Add(i+1,,,"Show me") ' Add a final child.
End Sub

Private Sub Form_Click()
   ' Tree will scroll and expand when you click the form.
   TreeView1.Nodes(TreeView1.Nodes.Count).EnsureVisible
End Sub