This documentation is archived and is not being maintained.

TreeView.SelectedNode Property

Gets or sets the tree node that is currently selected in the tree view control.

[Visual Basic]
Public Property SelectedNode As TreeNode
[C#]
public TreeNode SelectedNode {get; set;}
[C++]
public: __property TreeNode* get_SelectedNode();
public: __property void set_SelectedNode(TreeNode*);
[JScript]
public function get SelectedNode() : TreeNode;
public function set SelectedNode(TreeNode);

Property Value

The TreeNode that is currently selected in the tree view control.

Remarks

If no TreeNode is currently selected, the SelectedNode property is a null reference (Nothing in Visual Basic).

Example

[Visual Basic, C#, C++] The following example set the PathSeparator proprety of a TreeView and displays the number of child tree nodes contained in the TreeNodeCollection of the SelectedNode. The percentage of child tree node to total tree nodes in the tree view control is also displayed. This example assumes you have a Form with a Button, and a TreeView control with a TreeNodeCollection that has several TreeNode objects (preferably with three or more levels).

[Visual Basic] 
Private Sub myButton_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles myButton.Click
   ' Set the tre view's PathSeparator property.
   myTreeView.PathSeparator = "."

   ' Get the count of the child tree nodes contained in the SelectedNode.
   Dim myNodeCount As Integer = myTreeView.SelectedNode.GetNodeCount(True)
   Dim myChildPercentage As Decimal = CDec(myNodeCount) / _
      CDec(myTreeView.GetNodeCount(True)) * 100

   ' Display the tree node path and the number of child nodes it and the tree view have.
   MessageBox.Show(("The '" + myTreeView.SelectedNode.FullPath + "' node has " _
      + myNodeCount.ToString() + " child nodes." + Microsoft.VisualBasic.ControlChars.Lf _
      + "That is " + String.Format("{0:###.##}", myChildPercentage) _
      + "% of the total tree nodes in the tree view control."))
End Sub

[C#] 
private void myButton_Click(object sender, System.EventArgs e)
{
   // Set the tre view's PathSeparator property.
   myTreeView.PathSeparator = ".";

   // Get the count of the child tree nodes contained in the SelectedNode.
   int myNodeCount = myTreeView.SelectedNode.GetNodeCount(true);
   decimal myChildPercentage = ((decimal)myNodeCount/
     (decimal)myTreeView.GetNodeCount(true)) * 100;

   // Display the tree node path and the number of child nodes it and the tree view have.
   MessageBox.Show("The '" + myTreeView.SelectedNode.FullPath + "' node has " 
     + myNodeCount.ToString() + " child nodes.\nThat is " 
     + string.Format("{0:###.##}", myChildPercentage) 
     + "% of the total tree nodes in the tree view control.");
}

[C++] 
private:
    void myButton_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
        // Set the tre view's PathSeparator property.
        myTreeView->PathSeparator = S".";

        // Get the count of the child tree nodes contained in the SelectedNode.
        int myNodeCount = myTreeView->SelectedNode->GetNodeCount(true);
        Decimal myChildPercentage = ((Decimal)myNodeCount/
            (Decimal)myTreeView->GetNodeCount(true)) * 100;

        // Display the tree node path and the number of child nodes it and the tree view have.
        MessageBox::Show(String::Concat(S"The '", myTreeView->SelectedNode->FullPath,
            S"' node has ", __box(myNodeCount), S" child nodes.\nThat is ",
            String::Format(S"{0:###.##}", __box(myChildPercentage)),
            S"% of the total tree nodes in the tree view control."));
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeView Class | TreeView Members | System.Windows.Forms Namespace | TreeNode

Show: