HOW TO:使用 Windows Form TreeView 控制項加入和移除節點
Windows Form 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();
請參閱
工作
HOW TO:設定 Windows Form TreeView 控制項的圖示
HOW TO:逐一查看 Windows Form TreeView 控制項的所有節點
HOW TO:判斷按下哪個 TreeView 節點 (Windows Form)
HOW TO:將自訂資訊加入 TreeView 或 ListView 控制項 (Windows Form)