如何利用反射遍历某个页面下所有的控件的值
1个回答
展开全部
窗体上有四个控件,命令按钮command1。列表框list1。驱动器列表Drive1。文件夹列表Dir1。
驱动器和文件夹列表是用来选择你想要搜索的文件夹。
程序运行时选中你想要搜索的文件夹,单击搜索按钮。该文件夹下面所有的文件及子文件夹里面的文件都列到列表框中。
将代码复制到窗体即可。代码如下:
Private Sub Command1_Click()
List1.Clear
sosuofile (Dir1.List(Dir1.ListIndex))
MsgBox "搜索完毕!,共找到" + Str(List1.ListCount) + "条记录。", vbOKOnly + vbExclamation, "提示"
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Sub sosuofile(MyPath As String)
Dim Myname As String
Dim dir_i() As String
Dim i, idir As Long
If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While Myname <> ""
If Myname <> "." And Myname <> ".." Then
If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
idir = idir + 1
ReDim Preserve dir_i(idir) As String
dir_i(idir - 1) = Myname
Else: List1.AddItem "" & MyPath & " " & Myname '把找到的文件显示到列表框中
End If
End If
Myname = Dir '搜索下一项
Loop
For i = 0 To idir - 1
Call sosuofile(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String
End Sub
Private Sub Form_Load()
Command1.Caption = "搜索"
End Sub
驱动器和文件夹列表是用来选择你想要搜索的文件夹。
程序运行时选中你想要搜索的文件夹,单击搜索按钮。该文件夹下面所有的文件及子文件夹里面的文件都列到列表框中。
将代码复制到窗体即可。代码如下:
Private Sub Command1_Click()
List1.Clear
sosuofile (Dir1.List(Dir1.ListIndex))
MsgBox "搜索完毕!,共找到" + Str(List1.ListCount) + "条记录。", vbOKOnly + vbExclamation, "提示"
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Sub sosuofile(MyPath As String)
Dim Myname As String
Dim dir_i() As String
Dim i, idir As Long
If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While Myname <> ""
If Myname <> "." And Myname <> ".." Then
If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
idir = idir + 1
ReDim Preserve dir_i(idir) As String
dir_i(idir - 1) = Myname
Else: List1.AddItem "" & MyPath & " " & Myname '把找到的文件显示到列表框中
End If
End If
Myname = Dir '搜索下一项
Loop
For i = 0 To idir - 1
Call sosuofile(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String
End Sub
Private Sub Form_Load()
Command1.Caption = "搜索"
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询