'添加节点 Sub CreateTree(ByVal NodeText As String) ' Create the root node using the default constructor. Dim root As TreeNode = New TreeNode root.Text = NodeText ' Use the ChildNodes property of the root TreeNode to add child nodes. ' Create the node using the constructor that takes the text parameter. root.ChildNodes.Add(New TreeNode("Topic 1")) ' Create the node using the constructor that takes the text and value parameters. root.ChildNodes.Add(New TreeNode("Topic 2", "Value 2")) ' Create the node using the constructor that takes the text, value, ' and imageUrl parameters. root.ChildNodes.Add(New TreeNode("Topic 3", "Value 3", "Image1.jpg")) ' Create the node using the constructor that takes the text, value, ' imageUrl, navigateUrl, and target parameters. root.ChildNodes.Add(New TreeNode("Topic 4", "Value 4", "Image1.jpg", "http://www.microsoft.com", "_blank")) ' Add the root node to the Nodes collection of the TreeView control. DynamicTreeView.Nodes.Add(root) End Sub '删除节点 Private Sub treeView1_MouseDown(sender As Object, _ e As MouseEventArgs) Handles treeView1.MouseDown Select Case e.Button ' Remove the TreeNode under the mouse cursor ' if the right mouse button was clicked. Case MouseButtons.Right treeView1.GetNodeAt(e.X, e.Y).Remove() ' Remove the TreeNode under the mouse cursor ' if the middle mouse button (mouse wheel) was clicked. Case MouseButtons.Middle treeView1.GetNodeAt(e.X, e.Y).Toggle() End Select End Sub
'添加节点
Sub CreateTree(ByVal NodeText As String) ' Create the root node using the default constructor.
Dim root As TreeNode = New TreeNode
root.Text = NodeText ' Use the ChildNodes property of the root TreeNode to add child nodes.
' Create the node using the constructor that takes the text parameter.
root.ChildNodes.Add(New TreeNode("Topic 1")) ' Create the node using the constructor that takes the text and value parameters.
root.ChildNodes.Add(New TreeNode("Topic 2", "Value 2")) ' Create the node using the constructor that takes the text, value,
' and imageUrl parameters.
root.ChildNodes.Add(New TreeNode("Topic 3", "Value 3", "Image1.jpg")) ' Create the node using the constructor that takes the text, value,
' imageUrl, navigateUrl, and target parameters.
root.ChildNodes.Add(New TreeNode("Topic 4", "Value 4", "Image1.jpg", "http://www.microsoft.com", "_blank")) ' Add the root node to the Nodes collection of the TreeView control.
DynamicTreeView.Nodes.Add(root) End Sub
'删除节点
Private Sub treeView1_MouseDown(sender As Object, _
e As MouseEventArgs) Handles treeView1.MouseDown
Select Case e.Button
' Remove the TreeNode under the mouse cursor
' if the right mouse button was clicked.
Case MouseButtons.Right
treeView1.GetNodeAt(e.X, e.Y).Remove() ' Remove the TreeNode under the mouse cursor
' if the middle mouse button (mouse wheel) was clicked.
Case MouseButtons.Middle
treeView1.GetNodeAt(e.X, e.Y).Toggle()
End Select
End Sub