| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 538 人关注过本帖, 1 人收藏
标题:菜鸟看到树状视图控件看不懂了,能麻烦朋友帮我一条一条分析?
取消只看楼主 加入收藏
iloveai
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-3-30
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:1 
菜鸟看到树状视图控件看不懂了,能麻烦朋友帮我一条一条分析?
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
搜索更多相关主题的帖子: 朋友 根目录 
2013-03-30 08:13
iloveai
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-3-30
收藏
得分:0 
红色的是基本上一条都看不懂了
2013-03-30 08:13
快速回复:菜鸟看到树状视图控件看不懂了,能麻烦朋友帮我一条一条分析?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025463 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved