如何:添加和删除 Windows 窗体 TreeView 控件中的节点
更新:2007 年 11 月
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.get_SelectedNode().get_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.get_Nodes().Remove(treeView1.get_SelectedNode());
// Removes currently selected node, or root if nothing // is selected. treeView1->Nodes->Remove(treeView1->SelectedNode); // Clears all nodes. treeView1->Nodes->Clear();
请参见
任务
如何:设置 Windows 窗体 TreeView 控件的图标
如何:循环访问 Windows 窗体 TreeView 控件的所有节点
如何:确定被单击的 TreeView 节点(Windows 窗体)
如何:向 TreeView 或 ListView 控件添加自定义信息(Windows 窗体)