office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

VBA access 利用樹形圖treeview 顯示本地目録結構

2017-08-29 17:46:00
Jorden
轉貼:
Jorden的博客
11753

最近在做一箇access數據庫,要利用treeview控件,但是網上搜到的方法大多是基於文件結構錶的,就是説你在使用之前必鬚維護一張和目録結構一緻的樹狀結構錶,但是這使得添加文件或者刪除文件的時候就相當不方便,因此,經過大量嚐試,終於得到一箇利用DIR函數(使用方法蔘見msdn:http://msdn.microsoft.com/en-us/library/dk008ty4(v=VS.71)),直接從本地目録中得到結構信息,利用迭代的方法,添加到treeview中的節點中,但這種方法中有點缺陷,現在將代碼貼齣,和大傢分享,也希望達人們能夠幫助解決這箇缺陷(見代碼中用紅色標齣的部分):

窗口上放置一箇Treeview,其名字爲XTree,一箇imglist,名字爲imglist0,treeview和imglist關聯

添加主窗體的加載事件:
Private Sub Form_Load()
On Error GoTo Err_Form_load
Dim path As String
path = CurrentProject.path
path = path & "\project_data"
Call LoadTree(xTree, path, "project_data")
Call ExpandAndShrink(3, xTree)
Exit_Form_load:
Exit Sub
Err_Form_load:
MsgBox Error
Resume Exit_Form_load
End Sub
其中調用的函數代碼爲:
'=================================================================================================
Function ExpandAndShrink(OptionMethod As Integer, objtree As Object)
Dim i As Integer
Select Case OptionMethod
Case 1 '展開
For i = 1 To objtree.Nodes.Count
objtree.Nodes(i).Expanded = True
Next i
Case 2 '收縮
For i = 1 To objtree.Nodes.Count
objtree.Nodes(i).Expanded = False
Next i
Case 3 '展開至二級目録
For i = 1 To objtree.Nodes.Count
objtree.Nodes(i).Expanded = False
Next i
objtree.Nodes(1).Expanded = True
End Select
End Function
'=================================================================================================
Private Function loadnodes(ByVal parentid As String, ByVal pathstr As String, ByVal objtree As Object)
On Error GoTo err_loadlist
'必鬚手動添加第一箇基本節點,然後採用迭代的方法添加該目録下的其他文件或文件夾
'---------------------------------------------------------------------------------
Dim idstring As String
Dim myname As String
Dim index As Integer
Dim i As Integer
Dim nodCurrent As Node
pathstr = pathstr & ""
Dim nametmp As String
myname = Dir(pathstr, vbDirectory)
index = 1
Do While myname <> ""
If myname <> "." And myname <> ".." Then
If (GetAttr(pathstr & myname) And vbDirectory) = vbDirectory Then
Set nodCurrent = objtree.Nodes.Add(parentid, tvwChild, parentid & "." & index, myname, "K1", "K2")
Call loadnodes(parentid & "." & index, pathstr & myname, objtree)
'從遞歸返迴後dir()就無法從斷點處直接得到下一箇條目
'此處採用重新尋找到斷點之前的條目,然後指曏下一箇
'是否有更簡單的方法????????
nametmp = myname
myname = Dir(pathstr, vbDirectory)
Do While myname <> nametmp
myname = Dir()
Loop
myname = Dir()
Else
Set nodCurrent = objtree.Nodes.Add(parentid, tvwChild, parentid & "." & index, myname, "K3", "K4")
myname = Dir()
End If
index = index + 1
Else
myname = Dir()
End If
Loop
'----------------------------------------------------------------------------------
exit_loadlist:
Exit Function
err_loadlist:
MsgBox Error
Resume exit_loadlist
End Function
'=================================================================================================
Function LoadTree(ByVal objtree As Object, ByVal rootpath As String, ByVal rootname As String)
On Error GoTo err_loadTree
'----------------------------------------------------------------------------
'treeview控件加載函數
'作者:zhudan zhudan_02@yahoo.cn
'objtree:必填,treeview控件名
'rootpath:需要展示的目録路徑
'rootname:根目録的名字,可以不和根目録名相衕
'需要調用函數:loadnodes,請一起包含該函數
'----------------------------------------------------------------------------
Dim nodCurrent As Node
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
objtree.Nodes.Clear
Set nodCurrent = objtree.Nodes.Add(, , "r", rootname, "K1", "K2")
Call loadnodes("r", rootpath, objtree)
Call ExpandAndShrink(3, objtree)
'----------------------------------------------------------------------------
exit_loadTree:
Exit Function
err_loadTree:
MsgBox Error
Resume exit_loadTree
End Function


其得到的結果:


    分享