Como: Adicionar e remover nós com o controle TreeView dos Windows FormsHow to: Add and Remove Nodes with the Windows Forms TreeView Control

Os formulários do Windows TreeView controle armazena os nós de nível superior no seu Nodes coleção.The Windows Forms TreeView control stores the top-level nodes in its Nodes collection. Cada TreeNode também tem seu próprio Nodes coleção para armazenar seus nós filho.Each TreeNode also has its own Nodes collection to store its child nodes. Ambas as propriedades de coleção são do tipo TreeNodeCollection, que fornece a membros de coleção padrão que permitem adicionar, remover e reordenar os nós em um único nível da hierarquia de nós.Both collection properties are of type TreeNodeCollection, which provides standard collection members that enable you to add, remove, and rearrange the nodes at a single level of the node hierarchy.

Para adicionar nós de forma programáticaTo add nodes programmatically

  1. Use o Add método de exibição de árvore Nodes propriedade.Use the Add method of the tree view's Nodes property.

    ' Adds new node as a child node of the currently selected node.  
    Dim newNode As TreeNode = New TreeNode("Text for new node")  
    TreeView1.SelectedNode.Nodes.Add(newNode)  
    
    // Adds new node as a child node of the currently selected node.  
    TreeNode newNode = new TreeNode("Text for new node");  
    treeView1.SelectedNode.Nodes.Add(newNode);  
    
    // Adds new node as a child node of the currently selected node.  
    TreeNode ^ newNode = new TreeNode("Text for new node");  
    treeView1->SelectedNode->Nodes->Add(newNode);  
    

Para remover nós de forma programáticaTo remove nodes programmatically

  1. Use o Remove método de exibição de árvore Nodes propriedade para remover um único nó, ou o Clear método para limpar todos os nós.Use the Remove method of the tree view's Nodes property to remove a single node, or the Clear method to clear all nodes.

    ' Removes currently selected node, or root if nothing is selected.  
    TreeView1.Nodes.Remove(TreeView1.SelectedNode)  
    ' Clears all nodes.  
    TreeView1.Nodes.Clear()  
    
    // Removes currently selected node, or root if nothing   
    // is selected.  
    treeView1.Nodes.Remove(treeView1.SelectedNode);  
    // Clears all nodes.  
    TreeView1.Nodes.Clear();  
    
    // Removes currently selected node, or root if nothing  
    // is selected.  
    treeView1->Nodes->Remove(treeView1->SelectedNode);  
    // Clears all nodes.  
    treeView1->Nodes->Clear();  
    

Consulte tambémSee also