VB 如何让listview控件显示指定文件夹内的子文件夹和文件
1.显示子文件这步已经搞定,但是listbiew不能把子文件夹也显示出来,大侠们帮帮忙2.如果第一步搞定后,双击显示子文件夹后listview马上显示子文件夹的内容以上两...
1.显示子文件这步已经搞定,但是listbiew不能把子文件夹也显示出来,大侠们帮帮忙
2.如果第一步搞定后,双击显示子文件夹后listview 马上显示子文件夹的内容
以上两步请帮忙写一下代码,特别是第一步!!! TKS 展开
2.如果第一步搞定后,双击显示子文件夹后listview 马上显示子文件夹的内容
以上两步请帮忙写一下代码,特别是第一步!!! TKS 展开
2个回答
展开全部
Dim PathName As String '存放路径
Sub FolderList(PathName As String)
Dim sf As String
If Right(PathName, 1) <> "\" Then PathName = PathName & "\"
sf = Dir(PathName, vbDirectory) '获取第一个目录名
Do While sf <> ""
' 跳过当前的目录及上层目录
If sf <> "." And sf <> ".." Then
If (GetAttr(PathName + sf) And vbDirectory) = vbDirectory Then
Set ITM = ListView1.ListItems.Add(, , sf)
End If
End If
sf = Dir() ' 查找下一个目录
Loop
End Sub
Sub FileList(PathName As String)
Dim sDir As String
If Right(PathName, 1) <> "\" Then PathName = PathName & "\"
sDir = Dir(PathName & "*.*")
Do While Len(sDir) > 0
Set ITM = ListView1.ListItems.Add(, , sDir)
sDir = Dir
Loop
End Sub
Private Sub ShowAll()
PathName = "d:\vb" '这里我随便指定了一个初始目录
Call FolderList(PathName)
Call FileList(PathName)
End Sub
Private Sub Command1_Click()
Call ShowAll
End Sub
Private Sub Command2_Click()
ShowBack
End Sub
Private Sub Form_Load()
ListView1.ColumnHeaders.Add , , "文件", ListView1.Width
ListView1.View = lvwReport
ListView1.LabelEdit = lvwManual
End Sub
Private Sub ShowNext() '实现向下接着打开文件夹功能
PathName = PathName & ListView1.SelectedItem
Debug.Print PathName
ListView1.ListItems.Clear
Call FolderList(PathName)
Call FileList(PathName)
End Sub
Private Sub ShowBack() '实现向上功能
Dim sPathName() As String
Dim TempPath As String
Dim i As Integer
sPathName = Split(PathName, "\")
For i = 0 To UBound(sPathName) - 2
TempPath = TempPath & sPathName(i) & "\"
Next
PathName = TempPath
Debug.Print PathName
ListView1.ListItems.Clear
Call FolderList(PathName)
Call FileList(PathName)
End Sub
Private Sub ListView1_DblClick()
ShowNext
End Sub
Sub FolderList(PathName As String)
Dim sf As String
If Right(PathName, 1) <> "\" Then PathName = PathName & "\"
sf = Dir(PathName, vbDirectory) '获取第一个目录名
Do While sf <> ""
' 跳过当前的目录及上层目录
If sf <> "." And sf <> ".." Then
If (GetAttr(PathName + sf) And vbDirectory) = vbDirectory Then
Set ITM = ListView1.ListItems.Add(, , sf)
End If
End If
sf = Dir() ' 查找下一个目录
Loop
End Sub
Sub FileList(PathName As String)
Dim sDir As String
If Right(PathName, 1) <> "\" Then PathName = PathName & "\"
sDir = Dir(PathName & "*.*")
Do While Len(sDir) > 0
Set ITM = ListView1.ListItems.Add(, , sDir)
sDir = Dir
Loop
End Sub
Private Sub ShowAll()
PathName = "d:\vb" '这里我随便指定了一个初始目录
Call FolderList(PathName)
Call FileList(PathName)
End Sub
Private Sub Command1_Click()
Call ShowAll
End Sub
Private Sub Command2_Click()
ShowBack
End Sub
Private Sub Form_Load()
ListView1.ColumnHeaders.Add , , "文件", ListView1.Width
ListView1.View = lvwReport
ListView1.LabelEdit = lvwManual
End Sub
Private Sub ShowNext() '实现向下接着打开文件夹功能
PathName = PathName & ListView1.SelectedItem
Debug.Print PathName
ListView1.ListItems.Clear
Call FolderList(PathName)
Call FileList(PathName)
End Sub
Private Sub ShowBack() '实现向上功能
Dim sPathName() As String
Dim TempPath As String
Dim i As Integer
sPathName = Split(PathName, "\")
For i = 0 To UBound(sPathName) - 2
TempPath = TempPath & sPathName(i) & "\"
Next
PathName = TempPath
Debug.Print PathName
ListView1.ListItems.Clear
Call FolderList(PathName)
Call FileList(PathName)
End Sub
Private Sub ListView1_DblClick()
ShowNext
End Sub
追问
谢谢,以上两步都可以实现了。
怎么把文件及文件夹对应的图标显示出来呀,会不会很麻烦,给点代码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询