ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 12004|回复: 8

深入学习<树形目录控件——TreeView控件介绍>

[复制链接]

TA的精华主题

TA的得分主题

发表于 2010-4-3 14:11 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
http://club.excelhome.net/viewth ... 8%BC%FE%BD%E9%C9%DC
(9) singlesel属性:设置在树中选择新的条目时,是否展开此条目并收拢前一个条目,即设置为True时,并且当前选中的条目有子项的时候,会把子项展开,并将原来选中的条目收拢。
1.JPG
测试一下singlesel---前选中的条目有子项的时候,会把子项展开,并将原来选中的条目收拢

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-3 14:27 | 显示全部楼层
0.JPG 0.JPG
1.JPG
TreeView的数据准备用的是Sheet方法.
    '从工作表中获取数据作为一个数组
    With Sheets("Sheet1").Range(Sheets("Sheet1").[A2], Sheets("Sheet1").[A65536].End(xlUp))
        arrName = .Value
        arrParent = .Offset(, 1).Value
    End With

这做了一个矩阵,没搞清提高工作效率体现.

-----
关注点:
1 Indentation属性值决定。每一个缩进层次代表从列表边缘开始的10个像素宽,所以Indentation值为1的ComboItem将从Indentation为0的 ComboItem开始缩进10个像素。http://club.excelhome.net/thread-235182-1-1.html


2  其它二级节点没有三级节,这个节点能展开第三级节点的算法没搞清楚?
用递归法向节点添加数据,添加数据到节点中
if not exit(b) then ----- 是向二级节点添加添加节点?
1.JPG
没看清与三级节点的关系???

    For i = 1 To UBound(arrTemp)
        For j = 1 To UBound(arrTemp, 2)
            If Not IsEmpty(arrTemp(i, j)) Then
                With UserForm1.TreeView1
                    bExists = False
                    For Each elm In .Nodes
                        If elm = arrTemp(i, j) Then bExists = True
                    Next
                    If Not bExists Then
                        If j = 1 Then
                            Set node = .Nodes.Add(, , arrTemp(i, j), arrTemp(i, j), _
                            Image:=GetInfo(arrTemp(i, j), True))
                        Else
                            Set node = .Nodes.Add(arrTemp(i, j - 1), tvwChild, arrTemp(i, j), arrTemp(i, j), _
                            Image:=GetInfo(arrTemp(i, j), True))
                        End If
                        node.Expanded = True
                    End If
                End With
            End If
        Next

3 Image
以前没见过这种用法. 只知道向listImage控件添加图片.
    Dim myImgList As New ImageList
    Dim bt
    With myImgList.ListImages
        .Add Key:="Image1", Picture:=Sheet1.Image1.Picture
        .Add Key:="Image2", Picture:=Sheet1.Image2.Picture
        .Add Key:="Image3", Picture:=Sheet1.Image3.Picture
        .Add Key:="Image4", Picture:=Sheet1.Image4.Picture
        .Add Key:="Image5", Picture:=Sheet1.Image5.Picture
        .Add Key:="Image6", Picture:=Sheet1.Image6.Picture
        .Add Key:="Image7", Picture:=Sheet1.Image7.Picture
        .Add Key:="Image8", Picture:=Sheet1.Image8.Picture
        .Add Key:="Image9", Picture:=Sheet1.Image9.Picture
        .Add Key:="none", Picture:=Sheet1.none.Picture
    End With
    With TreeView1
        Set .ImageList = myImgList
        .Indentation = 14
        .LabelEdit = tvwManual
        .HideSelection = False
    End With
2.JPG
0.JPG
[localimg=400,196]2[/localimg]

以此为题,向各位大侠提的问题是,如何动态向Imagelist控件添加图片??

[ 本帖最后由 ningyong58 于 2010-4-3 15:44 编辑 ]
1.JPG

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-3 15:48 | 显示全部楼层
1 向各位大侠请教的问题是:子节点打勾,根节点同时打勾.下面的方法只是对子节点打勾.如何给根节点打勾??
0.JPG
Private Sub CommandButton2_Click()
  '设置为复选框显示
  TreeView1.CheckBoxes = True
End Sub

2 Treeview1_Nodecl和Treeview1_NodeCheck的用法.
0.JPG
原来感觉这个form比较深奥,现在看起来比较好理解了.

Private Sub Treeview1_Nodeclick(ByVal Node As MSComctlLib.Node)
  '返回对象路径
  Label3.Caption = Node.FullPath
End Sub

Private Sub Treeview1_NodeCheck(ByVal Node As MSComctlLib.Node)
  '复选框事件
  Label5.Caption = "当前选择的节点是:" & TreeView1.SelectedItem.Text
End Sub

Private Sub CommandButton2_Click()
  '设置为复选框显示
  TreeView1.CheckBoxes = True
End Sub

Private Sub CommandButton3_Click()
  '清除节点
  TreeView1.Nodes.Clear
End Sub

Private Sub CommandButton4_Click()
  '去掉复选框显示
  TreeView1.CheckBoxes = False
End Sub

Private Sub CommandButton5_Click()
  '开启热跟踪功能
  TreeView1.HotTracking = True
End Sub

Private Sub CommandButton6_Click()
  '编辑节点
  TreeView1.StartLabelEdit
End Sub

Private Sub CommandButton7_Click()
  '显示根节点连线
  TreeView1.LineStyle = tvwRootLines
End Sub

Private Sub CommandButton8_Click()
  '隐藏根节点连线
  TreeView1.LineStyle = tvwTreeLines
End Sub

Private Sub CommandButton9_Click()
  '移除所选节点
  '若为根节点,则将其子节点一并移除
  TreeView1.Nodes.Remove TreeView1.SelectedItem.Index
End Sub

Private Sub CommandButton10_Click()
  '统计节点个数
  Label1.Caption = "TreeView控件中节点对象的个数为:" & TreeView1.Nodes.Count & "个."
End Sub

Private Sub CommandButton11_Click()
  '将所选节点变为粗体
  TreeView1.SelectedItem.Bold = True
End Sub

Private Sub CommandButton12_Click()
  Dim i As Long
  For i = 1 To TreeView1.Nodes.Count
    TreeView1.Nodes(i).Expanded = True '展开所有节点
  Next i
End Sub

Private Sub CommandButton13_Click()
  Dim i As Long
  For i = 1 To TreeView1.Nodes.Count
    TreeView1.Nodes(i).Expanded = False '折叠所有节点
  Next i
End Sub
Private Sub OptionButton1_Click()
  '节点仅为文本
  TreeView1.Style = tvwTextOnly
End Sub

Private Sub OptionButton2_Click()
  '节点为图像文本
  TreeView1.Style = tvwPictureText
End Sub

Private Sub OptionButton3_Click()
  '节点为符号文本
  TreeView1.Style = tvwPlusMinusText
End Sub

Private Sub OptionButton4_Click()
  '节点为直线文本
  TreeView1.Style = tvwTreelinesText
End Sub

Private Sub OptionButton5_Click()
  '节点显示恢复正常
  TreeView1.Style = tvwTreelinesPlusMinusPictureText
End Sub

Private Sub Treeview1_Nodeclick(ByVal Node As MSComctlLib.Node)
  '返回对象路径
  Label3.Caption = Node.FullPath
End Sub

Private Sub Treeview1_NodeCheck(ByVal Node As MSComctlLib.Node)
  '复选框事件
  Label5.Caption = "当前选择的节点是:" & TreeView1.SelectedItem.Text
End Sub
在这个form中,到现在没搞清楚热跟踪的实际应用效果.
0.JPG

[ 本帖最后由 ningyong58 于 2010-4-3 16:44 编辑 ]

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-3 17:01 | 显示全部楼层
通过看下面图,基本搞清TreeView的层级关系了.
这种方法简单明了,能直观的建立三级以上的树形层级.
  '建立树形图
  Set nodX = SmartTreeView.Nodes.Add(, , "湖北省", "湖北省")
  Set nodX = SmartTreeView.Nodes.Add(, , "江苏省", "江苏省")
  
  Set nodX = SmartTreeView.Nodes.Add("湖北省", tvwChild, "武汉市", "武汉市")
  Set nodX = SmartTreeView.Nodes.Add("湖北省", tvwChild, "宜昌市", "宜昌市")
  Set nodX = SmartTreeView.Nodes.Add("武汉市", tvwChild, "wchild01", "江汉区")
  Set nodX = SmartTreeView.Nodes.Add("武汉市", tvwChild, "wchild02", "江岸区")
  Set nodX = SmartTreeView.Nodes.Add("武汉市", tvwChild, "wchild03", "汉口区")
  Set nodX = SmartTreeView.Nodes.Add("武汉市", tvwChild, "wchild04", "汉阳区")
0.JPG

节点还能作为文件存取.有意思
- <NODES>
  <NODE Caption="湖北省" Key="湖北省" Tag="" ParentKey="" />
  <NODE Caption="江苏省" Key="江苏省" Tag="" ParentKey="" />
  <NODE Caption="武汉市" Key="武汉市" Tag="" ParentKey="湖北省" />
  <NODE Caption="宜昌市" Key="宜昌市" Tag="" ParentKey="湖北省" />
    </NODES>
从下面程序看,TreeView好像下XML有密切的关系.XLM+TreeView是网页编制的主流.需要恶补.
  Dim xmlDoc As DOMDocument30
  Set xmlDoc = New DOMDocument30
  bttnPopulate.Enabled = False
  If Not xmlDoc.Load("C:\XMLNodes.xml") Then
    MsgBox "不能读取C:\XMLNodes.xml文件。"
    Exit Sub
  End If
  SmartTreeView.Nodes.Clear
  Dim iNode As Integer
  Dim newElement As IXMLDOMElement
  For iNode = 0 To xmlDoc.getElementsByTagName("NODE").Length - 1
    Set newElement = xmlDoc.getElementsByTagName("NODE").Item(iNode)
    If newElement.getAttribute("ParentKey") = "" Then
      SmartTreeView.Nodes.Add , , _
      newElement.getAttribute("Key"), _
      newElement.getAttribute("Caption")
    Else
      SmartTreeView.Nodes.Add _
      newElement.getAttribute("ParentKey"), _
      tvwChild, newElement.getAttribute("Key"), newElement.getAttribute("Caption")
    End If
  Next

[ 本帖最后由 ningyong58 于 2010-4-3 17:14 编辑 ]

TA的精华主题

TA的得分主题

发表于 2010-4-3 17:28 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2011-4-2 18:51 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2021-1-30 16:32 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2023-3-19 17:58 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-29 20:25 , Processed in 0.041036 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表