ACCESS TreeView控件的使用
一.在窗体的设计模式下,选择ActiveX控件,然后找到 Microsoft TreeView Control 6.0(SP6) ,确定
二.数据表的设计.重点在处理NodeID与ParentNodeID这两个字段的关系上.
三.TreeView数据的加载.下图是TreeView控件的所有事件.可以看到它本身是没有专用的加载事件的.需要在窗体的Load事件中或者其他事件处理TreeView的数据加载问题.
加载数据:
Tree.Nodes.Clear '先清空,不然Key 会重复 Dim Rec As DAO.Recordset Dim Node As Node Dim isql As String isql = "SELECT 'S'+MID(CSTR(100000+NodeID),2,5) AS NID ,'S'+MID(CSTR(100000+ParentNodeID),2,5) AS PID,NodeName FROM BomProList where 启用状态=-1" Set Rec = CurrentDb.OpenRecordset(isql) Rec.MoveLast n = Rec.RecordCount Rec.MoveFirst For i = 0 To n - 1 If Rec!PID = "S00000" Then If n = 1 Then Set Node1 = Tree.Nodes.Add(, , "S" & Rec!NID, "无此BOM数据") Else Set Node1 = Tree.Nodes.Add(, , "S" & Rec!NID, Rec!NodeName) End If Else Set Node1 = Tree.Nodes.Add("S" & Rec!PID, tvwChild, "S" & Rec!NID, Rec!NodeName) End If Rec.MoveNext Next '释放资源 Rec.Close Set Rec = Nothing
四.TreeView节点的折叠与展开
Function TreeExpanded(Tree, Bool) '设置节点的展开与缩放 Dim i As Long For i = 1 To Tree.Nodes.Count Tree.Nodes(i).Expanded = Bool 'True展开所有节点,False 收起所有节点 Next i If Bool Then Forms!BOM管理!Command80.Caption = "折叠" '设置按钮的状态 Tree.Nodes(1).Selected = True '展开后tree会被滚动到最底下的节点,需要后动设置,让滚动条回到tree的第一个节点 Else Forms!BOM管理!Command80.Caption = "展开" End If End Function