TreeView.SelectedNode 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定目前在樹狀檢視控制中選取的樹節點。
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)]
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 that。
- 屬性
範例
以下程式碼範例設定 PathSeparator 了 a TreeView 的屬性,並顯示包含在 TreeNodeCollectionSelectedNode的子樹節點數量。 在樹檢視控制中,子樹節點佔總樹節點的百分比也會被顯示。 這個例子需要你有一個 Form ,且TreeView有一個控制Button點,且該控制TreeNodeCollection點包含多個TreeNode物件(最好有三個或以上層級)。
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
備註
若目前選擇 no TreeNode ,則性質 SelectedNode 為 null。
當你設定這個屬性時,指定的節點會捲入檢視,並展開任何父節點,使指定的節點可見。
當父節點或所選節點的任何祖先節點被程式化或使用者操作收斂時,該倒塌的節點即成為被選中的節點。