VBMAN.Dialog - 文件对话框对象
概述
VBMAN.Dialog 提供了 Windows 标准文件对话框功能,包括打开文件、保存文件、选择文件夹等。
核心特性
- 打开文件对话框: 支持单选/多选、文件过滤
- 保存文件对话框: 支持覆盖提示、默认扩展名
- 文件夹浏览对话框: 支持新建对话框样式
- 属性配置: 通过属性设置对话框选项
属性
| 属性 | 类型 | 说明 |
|---|---|---|
DialogTitle | String | 对话框标题 |
InitialDir | String | 初始目录 |
DefaultExt | String | 默认扩展名 |
FileName | String | 默认/返回文件名 |
Filter | String | 文件过滤器 |
MultiSelect | Boolean | 允许多选(默认 False) |
OverwritePrompt | Boolean | 覆盖提示(默认 True) |
PathMustExist | Boolean | 路径必须存在(默认 True) |
FileMustExist | Boolean | 文件必须存在(默认 True) |
HideReadOnly | Boolean | 隐藏只读选项(默认 True) |
方法
ShowOpen
显示打开文件对话框
vb
Public Function ShowOpen() As Variant返回: 选中的文件路径,取消返回 Empty
示例:
vb
With VBMAN.Dialog
.DialogTitle = "选择文件"
.InitialDir = "C:\\"
.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"
.FileName = ""
Dim result As Variant
result = .ShowOpen
If Not IsEmpty(result) Then
MsgBox "选择了: " & result
End If
End WithShowSave
显示保存文件对话框
vb
Public Function ShowSave() As String返回: 保存的文件路径,取消返回空字符串
示例:
vb
With VBMAN.Dialog
.DialogTitle = "保存文件"
.InitialDir = App.Path
.DefaultExt = "txt"
.Filter = "文本文件 (*.txt)|*.txt"
.FileName = "新建文件.txt"
Dim savePath As String
savePath = .ShowSave
If savePath <> "" Then
' 保存文件...
End If
End WithShowBrowseForFolder
显示文件夹浏览对话框
vb
Public Function ShowBrowseForFolder() As String返回: 选择的文件夹路径,取消返回空字符串
示例:
vb
With VBMAN.Dialog
.DialogTitle = "选择文件夹"
Dim folderPath As String
folderPath = .ShowBrowseForFolder
If folderPath <> "" Then
MsgBox "选择了: " & folderPath
End If
End WithSelectFiles
选择多个文件返回集合
vb
Public Function SelectFiles() As cCollection返回: 选中文件路径的集合
示例:
vb
With VBMAN.Dialog
.DialogTitle = "选择多个文件"
.MultiSelect = True
.Filter = "所有文件 (*.*)|*.*"
Dim files As cCollection
Set files = .SelectFiles
Dim i As Long
For i = 1 To files.Count
Debug.Print files(i)
Next i
End With综合示例
示例1: 文本文件打开
vb
Private Sub OpenTextFile()
With VBMAN.Dialog
.DialogTitle = "打开文本文件"
.InitialDir = App.Path
.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"
.FileMustExist = True
Dim filePath As Variant
filePath = .ShowOpen
If Not IsEmpty(filePath) Then
Dim content As String
content = VBMAN.ToolsStream.LoadFileAsText(CStr(filePath))
Text1.Text = content
End If
End With
End Sub示例2: 保存配置
vb
Private Sub SaveConfig()
With VBMAN.Dialog
.DialogTitle = "保存配置"
.InitialDir = App.Path
.DefaultExt = "ini"
.Filter = "配置文件 (*.ini)|*.ini"
.OverwritePrompt = True
.FileName = "config.ini"
Dim savePath As String
savePath = .ShowSave
If savePath <> "" Then
VBMAN.Ini.SaveTo savePath
End If
End With
End Sub示例3: 选择输出目录
vb
Private Sub SelectOutputDir()
With VBMAN.Dialog
.DialogTitle = "选择输出目录"
.InitialDir = "C:\\"
Dim outputDir As String
outputDir = .ShowBrowseForFolder
If outputDir <> "" Then
LabelOutput.Caption = outputDir
End If
End With
End Sub最佳实践
- 设置初始目录: 使用 App.Path 或用户上次选择的目录
- 文件过滤: 提供合理的文件类型过滤选项
- 错误处理: 检查返回值是否为空/Empty
- 用户体验: 设置有意义的对话框标题