[示例一] TreeView控件常用方法、属性和事件示例(1) 在VBE编辑器中插入一个用户窗体,并在该窗体上添加一些控件,即一个TreeView控件、一个ImageList控件、一些命令按钮控件、一些选项按钮控件和一些标签控件,如图4所示。 图4:用户窗体界面 在用户窗体代码窗口,输入下面的代码: Private Sub UserForm_Initialize() '初始化ImageList控件,添加图片 Dim img As New ImageList img.ListImages.Add 1, "book1", LoadPicture(ThisWorkbook.Path & "\book1.jpg") img.ListImages.Add 2, "book2", LoadPicture(ThisWorkbook.Path & "\book2.jpg") img.ListImages.Add 3, "book3", LoadPicture(ThisWorkbook.Path & "\book3.jpg") Set TreeView1.ImageList = img '设置显示节点路径时的分隔符 TreeView1.PathSeparator = "\" End Sub Private Sub CommandButton1_Click() '添加节点 Dim NodeX As Node TreeView1.Nodes.Clear Set NodeX = TreeView1.Nodes.Add(, , "课程科目", "课程科目", "book3") Set NodeX = TreeView1.Nodes.Add("课程科目", tvwChild, "语文", "语文", "book1") Set NodeX = TreeView1.Nodes.Add("课程科目", tvwChild, "数学", "数学", "book1") Set NodeX = TreeView1.Nodes.Add("课程科目", tvwChild, "外语", "外语", "book1") Set NodeX = TreeView1.Nodes.Add("课程科目", tvwChild, "政治", "政治", "book1") Set NodeX = TreeView1.Nodes.Add("课程科目", tvwChild, "物理", "物理", "book1") Set NodeX = TreeView1.Nodes.Add("课程科目", tvwChild, "化学", "化学", "book1") Set NodeX = TreeView1.Nodes.Add("课程科目", tvwChild, "生物", "生物", "book1") 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 下面是该用户窗体的一些运行结果截图。 图5:添加节点并展开节点。 图6:为节点加上复选框。 图7:选中复选框后,下面显示相应的信息。 图8:选中某节点后显示相应的节点位置信息。 图9:编辑某节点 图10:显示根节点连线 图11:将节点变为粗体
图12:选择节点仅为文本后(其它选项按钮的效果用户可以自已体验)
|