如何:添加和删除 Windows 窗体 TreeView 控件中的节点
Windows 窗体 TreeView 控件存储其 Nodes 集合中的顶级节点。 每个 TreeNode 都有自己的 Nodes 集合来存储其子节点。 这两个集合属性的类型均为 TreeNodeCollection,它提供了标准集合成员,使你能够在节点层次结构的单个级别添加、删除和重新排列节点。
以编程方式添加节点
-
' 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);
以编程方式删除节点
使用数视图的 Nodes 属性的 Remove 方法删除单个节点,或使用 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();
// Removes currently selected node, or root if nothing // is selected. treeView1->Nodes->Remove(treeView1->SelectedNode); // Clears all nodes. treeView1->Nodes->Clear();