Skip to content

VBMAN.Dialog - 文件对话框对象

概述

VBMAN.Dialog 提供了 Windows 标准文件对话框功能,包括打开文件、保存文件、选择文件夹等。

核心特性

  • 打开文件对话框: 支持单选/多选、文件过滤
  • 保存文件对话框: 支持覆盖提示、默认扩展名
  • 文件夹浏览对话框: 支持新建对话框样式
  • 属性配置: 通过属性设置对话框选项

属性

属性类型说明
DialogTitleString对话框标题
InitialDirString初始目录
DefaultExtString默认扩展名
FileNameString默认/返回文件名
FilterString文件过滤器
MultiSelectBoolean允许多选(默认 False)
OverwritePromptBoolean覆盖提示(默认 True)
PathMustExistBoolean路径必须存在(默认 True)
FileMustExistBoolean文件必须存在(默认 True)
HideReadOnlyBoolean隐藏只读选项(默认 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 With

ShowSave

显示保存文件对话框

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 With

ShowBrowseForFolder

显示文件夹浏览对话框

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 With

SelectFiles

选择多个文件返回集合

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

最佳实践

  1. 设置初始目录: 使用 App.Path 或用户上次选择的目录
  2. 文件过滤: 提供合理的文件类型过滤选项
  3. 错误处理: 检查返回值是否为空/Empty
  4. 用户体验: 设置有意义的对话框标题

VB6及其LOGO版权为微软公司所有