注册 登录
编程论坛 VB6论坛

菜鸟看到树状视图控件看不懂了,能麻烦朋友帮我一条一条分析?

iloveai 发布于 2013-03-30 08:13, 538 次点击
Private Sub Form_Load()
     ' 初始化控件的内容
    Call InitTreeData
    TreeView1.Nodes(1).Selected = True    ' 根节点为当前节点
    TreeView1.Nodes(1).EnsureVisible      ' 根节点可见
End Sub
' 初始化控件的内容
Private Sub InitTreeData()
    Dim i As Integer '定义变量
    ' 增加节点
    TreeView1.Nodes.Add , , "根目录", "根目录" '添加根目录
    For i = 1 To 5 '添加1级目录
        TreeView1.Nodes.Add "根目录", tvwChild, "1级目录" & CStr(i), "1级目录" & CStr(i)
    Next i
    For i = 1 To 5 '1级目录1中添加2级目录
        TreeView1.Nodes.Add "1级目录1", tvwChild, "2级目录1" & CStr(i), "2级目录" & CStr(i)
    Next i
    For i = 1 To 5 '1级目录3中添加2级目录
        TreeView1.Nodes.Add "1级目录3", tvwChild, "2级目录2" & CStr(i), "2级目录" & CStr(i)
    Next i
    For i = 1 To 5 '1级目录5中添加2级目录
TreeView1.Nodes.Add "1级目录5", tvwChild, "2级目录3" & CStr(i), "2级目录" & CStr(i)
    Next i
    For i = 1 To 5 '2级目录11中添加3级目录                                               (从这里开始看不懂了?哪里出现了2级目录11?,尤其下面更看不懂,能帮我逐条分析一下?谢谢了)
        TreeView1.Nodes.Add "2级目录11", tvwChild, "3级目录" & CStr(i), "3级目录" & CStr(i)
    Next i
    ' 将节点展开
    TreeView1.Nodes(1).Expanded = True
End Sub
' 选取节点
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
     gCheckChildrenBySelf TreeView1, Node.Index, Node.Checked
     gCheckParentBySibling TreeView1, Node.Index
End Sub
' 根据自身选取情况,确定全选或取消其下所有子项
Private Sub gCheckChildrenBySelf(TreeView1 As TreeView, ByVal curIndex As Integer, ByVal bCh As Integer)
    Dim n As Integer
    If TreeView1.Nodes(curIndex).Children <= 0 Then
        Exit Sub
    Else
        n = TreeView1.Nodes(curIndex).Child.Index
        Do While n <> TreeView1.Nodes(curIndex).Child.LastSibling.Index
            TreeView1.Nodes(n).Checked = bCh
            gCheckChildrenBySelf TreeView1, n, bCh
            n = TreeView1.Nodes(n).Next.Index
        Loop
        TreeView1.Nodes(n).Checked = bCh
        gCheckChildrenBySelf TreeView1, n, bCh
    End If
End Sub
' 根据同层、同父节点的选取情况,确定是否选取其父节点(乃至更上层),直至根节点
Private Sub gCheckParentBySibling(TreeView1 As TreeView, ByVal curIndex As Integer)
    Dim n As Integer
    Dim bHaveChecked As Boolean
    If TreeView1.Nodes(curIndex).FirstSibling.Index = 1 Then
        Exit Sub
    Else
        bHaveChecked = False
        n = TreeView1.Nodes(curIndex).FirstSibling.Index
        Do While n <> TreeView1.Nodes(curIndex).LastSibling.Index
Exit Do
            If TreeView1.Nodes(n).Checked = True Then
                bHaveChecked = True
           End If
            n = TreeView1.Nodes(n).Next.Index
        Loop
        If TreeView1.Nodes(n).Checked = True Then
            bHaveChecked = True
        End If
        
        If bHaveChecked = True Then
            TreeView1.Nodes(curIndex).Parent.Checked = vbChecked
        Else
            TreeView1.Nodes(curIndex).Parent.Checked = vbUnchecked
        End If
        gCheckParentBySibling TreeView1, TreeView1.Nodes(curIndex).Parent.Index
    End If
End Sub
2 回复
#2
iloveai2013-03-30 08:13
红色的是基本上一条都看不懂了
#3
seafish0112013-03-31 04:47
几乎很少写注释
1