Freigeben über


TreeView.SelectedNode-Eigenschaft

Ruft den derzeit im Strukturansicht-Steuerelement ausgewählten Strukturknoten ab oder legt diesen fest.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Property SelectedNode As TreeNode
'Usage
Dim instance As TreeView
Dim value As TreeNode

value = instance.SelectedNode

instance.SelectedNode = value
public TreeNode SelectedNode { get; set; }
public:
property TreeNode^ SelectedNode {
    TreeNode^ get ();
    void set (TreeNode^ value);
}
/** @property */
public TreeNode get_SelectedNode ()

/** @property */
public void set_SelectedNode (TreeNode value)
public function get SelectedNode () : TreeNode

public function set SelectedNode (value : TreeNode)

Eigenschaftenwert

Der derzeit im Strukturansicht-Steuerelement ausgewählte TreeNode.

Hinweise

Wenn derzeit kein TreeNode ausgewählt ist, ist die SelectedNode-Eigenschaft NULL (Nothing in Visual Basic).

Wenn Sie diese Eigenschaft festlegen, wird der angegebene Knoten durch Bildlauf angezeigt, und übergeordnete Knoten werden erweitert, sodass der angegebene Knoten sichtbar ist.

Wenn der übergeordnete Knoten oder ein Vorgängerknoten des ausgewählten Knotens programmgesteuert oder durch eine Benutzeraktion reduziert wird, wird der reduzierte Knoten zum ausgewählten Knoten.

Beispiel

Im folgenden Codebeispiel wird die PathSeparator-Eigenschaft einer TreeView festgelegt, und die Anzahl der untergeordneten Strukturknoten in der TreeNodeCollection der SelectedNode wird angezeigt. Der prozentuale Anteil der untergeordneten Strukturknoten an der Gesamtzahl der Strukturknoten im Strukturansicht-Steuerelement wird ebenfalls angezeigt. Für dieses Beispiel ist es erforderlich, dass ein Form mit einer Button sowie ein TreeView-Steuerelement mit einer TreeNodeCollection mit mehreren TreeNode-Objekten (vorzugsweise mit drei oder vier Ebenen) vorhanden sind.

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
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.");
}
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( 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 tre view's PathSeparator property.
    myTreeView.set_PathSeparator(".");
    // Get the count of the child tree nodes contained in the SelectedNode.
    int myNodeCount = myTreeView.get_SelectedNode().GetNodeCount(true);
    System.Decimal myChildPercentage =
        Decimal.Multiply(Decimal.Divide(new Decimal(myNodeCount),
        new Decimal(myTreeView.GetNodeCount(true))), new Decimal(100));
    // Display the tree node path and the number of child nodes it and the
    // tree view have.
    MessageBox.Show("The '" + myTreeView.get_SelectedNode().get_FullPath()
        + "' node has " + ((Int32)myNodeCount).ToString()
        + " child nodes.\nThat is " + String.Format("{0:###.##}",
        myChildPercentage)
        + "% of the total tree nodes in the tree view control.");
} //myButton_Click

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

TreeView-Klasse
TreeView-Member
System.Windows.Forms-Namespace
TreeNode-Klasse