vb.net 教程 3-4 窗体编程 公共控件10 TreeView 3

上一节学习了从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

删除根节点:
    '删除根节点
    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 教程 目录

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...