TreeView.SelectedNode 属性

定义

获取或设置当前在树视图控件中选定的树节点。Gets or sets the tree node that is currently selected in the tree view control.

public:
 property System::Windows::Forms::TreeNode ^ SelectedNode { System::Windows::Forms::TreeNode ^ get(); void set(System::Windows::Forms::TreeNode ^ value); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.TreeNode SelectedNode { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedNode : System.Windows.Forms.TreeNode with get, set
Public Property SelectedNode As TreeNode

属性值

TreeNode

当前在树视图控件中选定的 TreeNodeThe TreeNode that is currently selected in the tree view control.

属性

示例

下面的代码示例设置的 PathSeparator 属性 TreeView ,并显示包含在中的子树节点的数目 TreeNodeCollection SelectedNodeThe following code example sets the PathSeparator property of a TreeView and displays the number of child tree nodes that is 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. 此示例要求具有 Form 具有的 Button 和具有 TreeView 多个对象的控件, TreeNodeCollection 这些 TreeNode 对象 (最好具有三个或更多的) 级别。This example requires that 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).

void myButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{

   // Set the tree 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( String::Concat( "The '", myTreeView->SelectedNode->FullPath, "' node has ", myNodeCount, " child nodes.\nThat is ", String::Format( "{0:###.##}", myChildPercentage ), "% of the total tree nodes in the tree view control." ) );
}
private void myButton_Click(object sender, System.EventArgs e)
{
   // Set the tree 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.");
}
Private Sub myButton_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles myButton.Click
   ' Set the tree 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

注解

如果 TreeNode 当前未选择任何,则 SelectedNode 属性为 nullIf no TreeNode is currently selected, the SelectedNode property is null.

设置此属性时,指定的节点将滚动到视图中,并展开任何父节点,以使指定的节点可见。When you set this property, the specified node is scrolled into view and any parent nodes are expanded so that the specified node is visible.

当所选节点的父节点或任意祖先节点以编程方式或通过用户操作折叠时,折叠的节点将成为所选节点。When the parent node or any ancestor node of the selected node is collapsed either programmatically or through user action, the collapsed node becomes the selected node.

适用于

另请参阅