Freigeben über


TreeNodeCollection.Clear-Methode

Entfernt alle Strukturknoten aus der Auflistung.

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

Syntax

'Declaration
Public Overridable Sub Clear
'Usage
Dim instance As TreeNodeCollection

instance.Clear
public virtual void Clear ()
public:
virtual void Clear ()
public void Clear ()
public function Clear ()

Hinweise

Mit dieser Methode können Sie die gesamte Auflistung von Strukturknoten aus einer Strukturansicht löschen.

Mit der Remove-Methode oder der RemoveAt-Methode können Sie einen einzelnen Strukturknoten aus der Auflistung entfernen.

Verwenden Sie die Add-Methode, die AddRange-Methode oder die Insert-Methode, um der Auflistung neue TreeNode-Objekte hinzuzufügen.

Beispiel

Im folgenden Codebeispiel wird die TreeNodeCollection aus einer TreeView in ein temporäres Array kopiert. Anschließend wird mithilfe der AddRange-Methode der Inhalt des Arrays in eine andere TreeView kopiert. Die TreeNodeCollection aus der Quell-TreeView wird mithilfe der Clear-Methode gelöscht. Für dieses Beispiel ist es erforderlich, dass zwei TreeView-Steuerelemente vorhanden sind, wovon eines eine Auflistung von TreeNode-Objekten enthält.

Private Sub MyButtonAddAllClick(sender As Object, e As EventArgs)
   ' Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
   Dim myTreeNodeCollection As TreeNodeCollection = myTreeViewBase.Nodes
   ' Create an array of 'TreeNodes'.
   Dim myTreeNodeArray(myTreeViewBase.Nodes.Count-1) As TreeNode
   ' Copy the tree nodes to the 'myTreeNodeArray' array.
   myTreeViewBase.Nodes.CopyTo(myTreeNodeArray, 0)
   ' Remove all the tree nodes from the 'myTreeViewBase' TreeView.
   myTreeViewBase.Nodes.Clear()
   ' Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
      myTreeViewCustom.Nodes.AddRange(myTreeNodeArray)
End Sub
private void MyButtonAddAllClick(object sender, EventArgs e)
{
   // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
   TreeNodeCollection myTreeNodeCollection = myTreeViewBase.Nodes;
   // Create an array of 'TreeNodes'.
   TreeNode[] myTreeNodeArray = new TreeNode[myTreeViewBase.Nodes.Count];
   // Copy the tree nodes to the 'myTreeNodeArray' array.
   myTreeViewBase.Nodes.CopyTo(myTreeNodeArray,0);
   // Remove all the tree nodes from the 'myTreeViewBase' TreeView.
   myTreeViewBase.Nodes.Clear();
   // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
   myTreeViewCustom.Nodes.AddRange(myTreeNodeArray);
}
private:
   void MyButtonAddAllClick( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      
      // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
      TreeNodeCollection^ myTreeNodeCollection = myTreeViewBase->Nodes;
      
      // Create an array of 'TreeNodes'.
      array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(myTreeViewBase->Nodes->Count);
      
      // Copy the tree nodes to the 'myTreeNodeArray' array.
      myTreeViewBase->Nodes->CopyTo( myTreeNodeArray, 0 );
      
      // Remove all the tree nodes from the 'myTreeViewBase' TreeView.
      myTreeViewBase->Nodes->Clear();
      
      // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
      myTreeViewCustom->Nodes->AddRange( myTreeNodeArray );
   }
private void myButtonAddAllClick(Object sender, EventArgs e)
{
    // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
    TreeNodeCollection myTreeNodeCollection = myTreeViewBase.get_Nodes();
    // Create an array of 'TreeNodes'.
    TreeNode myTreeNodeArray[] =
        new TreeNode[myTreeViewBase.get_Nodes().get_Count()];
    // Copy the tree nodes to the 'myTreeNodeArray' array.
    myTreeViewBase.get_Nodes().CopyTo(myTreeNodeArray, 0);
    // Remove all the tree nodes from the 'myTreeViewBase' TreeView.
    myTreeViewBase.get_Nodes().Clear();
    // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
    myTreeViewCustom.get_Nodes().AddRange(myTreeNodeArray);
} //myButtonAddAllClick

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

TreeNodeCollection-Klasse
TreeNodeCollection-Member
System.Windows.Forms-Namespace