上一节学习了从vs中利用编辑器手动编辑节点,本节主要谈谈使用代码来编辑节点,实际也就是对TreeNode的操作。
先看看窗体中控件的布局:
为了区别根节点和子节点,特地做了点区分。
窗体载入的时候,把ImageList中的图片载入4个ListView中,
ListView的SmallImageList属性设置为ImageList1;View属性设置为SmallIcon:
Private Sub Form7_Load(sender As Object,e As EventArgs) Handles MyBase.Load For i As Integer = 0 To ImageList1.Images.Count - 1 ListView1.Items.Add(New ListViewItem("",i)) ListView2.Items.Add(New ListViewItem("",i)) ListView3.Items.Add(New ListViewItem("",i)) ListView4.Items.Add(New ListViewItem("",i)) Next End Sub
添加根节点:
'添加根节点 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim strNodeName As String = TextBox1.Text If ListView1.SelectedIndices.Count = 0 OrElse ListView2.SelectedIndices.Count = 0 Then MessageBox.Show("未选择根节点图标") Exit Sub End If Dim tnRoot As New TreeNode(strNodeName) tnRoot.ImageIndex = ListView1.SelectedIndices(0) tnRoot.SelectedImageIndex = ListView2.SelectedIndices(0) TreeView1.Nodes.Add(tnRoot) End Sub
其中TreeNode的构造函数之一:
然后再增加显示的图标(tnRoot.ImageIndex =……)和选中时候的图标(tnRoot.SelectedImageIndex =……)
同时请注意,判断是否已经在ListView1和ListView2中选择图标的代码:
If ListView2.SelectedIndices.Count = 0 OrElse ListView2.SelectedIndices.Count = 0 Then
删除根节点:
@H_301_32@ '删除根节点
Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click
If IsNothing(TreeView1.SelectedNode) Then Exit Sub
Dim tnSelectedNode As TreeNode = TreeView1.SelectedNode
If tnSelectedNode.Level = 0 Then
TreeView1.Nodes.Remove(tnSelectedNode)
Else
MessageBox.Show("此按钮只用于删除根节点")
End If
End Sub
判断是否选中有节点的代码:
IsNothing(TreeView1.SelectedNode)
判断节点等级的代码:
tnSelectedNode.Level
如果是根节点,那么Level=0。
清除根节点,也就是清空TreeView:
'清除根节点 Private Sub Button3_Click(sender As Object,e As EventArgs) Handles Button3.Click TreeView1.Nodes.Clear() End Sub
同样,子节点的操作:
添加子节点:
'添加子节点 Private Sub Button6_Click(sender As Object,e As EventArgs) Handles Button6.Click If IsNothing(TreeView1.SelectedNode) Then Exit Sub Dim tnSelectedNode As TreeNode = TreeView1.SelectedNode If ListView3.SelectedIndices.Count = 0 OrElse ListView4.SelectedIndices.Count = 0 Then MessageBox.Show("未选择节点图标") Exit Sub End If Dim strNodeName As String = TextBox2.Text Dim tnAddNode As New TreeNode(strNodeName,ListView3.SelectedIndices(0),ListView4.SelectedIndices(0)) tnSelectedNode.Nodes.Add(tnAddNode) End Sub
首先判断是否已经选择了一个节点,如果选择了再添加:
If IsNothing(TreeView1.SelectedNode) Then Exit Sub
这里用了TreeNode的另外一个构造函数
Dim tnAddNode As New TreeNode(strNodeName,ListView4.SelectedIndices(0))
删除节点:
'删除普通节点 Private Sub Button5_Click(sender As Object,e As EventArgs) Handles Button5.Click If IsNothing(TreeView1.SelectedNode) Then Exit Sub Dim tnSelectedNode As TreeNode = TreeView1.SelectedNode If tnSelectedNode.Level <> 0 Then TreeView1.Nodes.Remove(tnSelectedNode) Else MessageBox.Show("此按钮只用于删除普通节点") End If End Sub清除子节点:
'清除当前节点下的所有子节点 Private Sub Button4_Click(sender As Object,e As EventArgs) Handles Button4.Click If IsNothing(TreeView1.SelectedNode) Then Exit Sub Dim tnSelectedNode As TreeNode = TreeView1.SelectedNode If tnSelectedNode.Nodes.Count > 0 Then tnSelectedNode.Nodes.Clear() Else MessageBox.Show("此节点下没有子节点") End If End Sub
学习更多vb.net知识,请参看 vb.net 教程 目录