Share via

TreeNode.EnsureVisible Method


Ensures that the tree node is visible, expanding tree nodes and scrolling the tree view control as necessary.

 void EnsureVisible();
public void EnsureVisible ();
member this.EnsureVisible : unit -> unit
Public Sub EnsureVisible ()


The following code example brings the last child tree node of the last root tree node into view in the tree view when a button is clicked. This example requires that you have a TreeView control on a Form that contains a collection of TreeNode objects, and a Button. There should be enough tree nodes so that they are not all visible in the tree view control.

void button3_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   TreeNode^ lastNode = treeView1->Nodes[ treeView1->Nodes->Count - 1 ]->Nodes[ treeView1->Nodes[ treeView1->Nodes->Count - 1 ]->Nodes->Count - 1 ];
   if (  !lastNode->IsVisible )
      MessageBox::Show( String::Concat( lastNode->Text, " tree node is visible." ) );
private void button3_Click(object sender, System.EventArgs e)
   TreeNode lastNode = treeView1.Nodes[treeView1.Nodes.Count - 1].
     Nodes[treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Count - 1];

   if (!lastNode.IsVisible)
      MessageBox.Show(lastNode.Text + " tree node is visible.");
Private Sub button3_Click(sender As Object, _
  e As System.EventArgs) Handles button3.Click
   Dim lastNode as TreeNode
   lastNode = treeView1.Nodes(treeView1.Nodes.Count - 1). _
     Nodes(treeView1.Nodes(treeView1.Nodes.Count - 1).Nodes.Count - 1)

   If Not lastNode.IsVisible Then
      MessageBox.Show(lastNode.Text & _
        " tree node is visible.")
   End If
End Sub


When the EnsureVisible method is called, the tree is expanded and scrolled to ensure that the current tree node is visible in the TreeView. This method is useful if you are selecting a tree node in code based on certain criteria. By calling this method after you select the node, the user can see and interact with the selected node.


If the TreeView.ItemHeight property is set to a value that is larger than the height of the tree view control, calling this method has unexpected results.

Applies to

See also