vb 关于commondialog的多选

这篇博客介绍了在VB中使用CommonDialog控件进行文件多选操作的方法。提供了两种不同的函数实现,用于提取所选文件的路径和名称。函数会根据是否允许多选来设置Flags,并通过处理返回的文件名字符串来获取每个文件的信息。
摘要由CSDN通过智能技术生成

关于commondialog的多选(如何提取文件名?)

 第一种方法==================================================

Function OpenFile(strFileInfor As String, strFileType As String, blnIsMulti As Boolean, OpenFileDialog As Control) As String()
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'OpenFileDialog         通用对话框
    'strstrfileInfor       文件提示
    'strstrfileType         文件类型,扩展名
    'blnblnismulti           是否为多选
    '返回为文件名数组,OpenFile(0)存放文件路径,从1开始存放文件名,包括只有一个文件时的情况。
    '返回的路径中已经包含了最后的"\"
    '如果没有选择任何文件,OpenFileFuction(0)返回为""
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '设置打开文件对话框过滤器
    OpenFileDialog.Filter = strFileInfor & "|" & strFileType
    '设定是否可以多选
    If blnIsMulti Then
        OpenFileDialog.Flags = &H80200
        OpenFileDialog.MaxFileSize = 1024         '设置使用CommonDialog控件被打开的文件名的最大尺寸
    Else
        OpenFileDialog.Flags = 0
    End If
    OpenFileDialog.ShowOpen
    '提取各文件名
    Dim tempStr     As String
    Dim tempFileName()     As String
    tempStr = OpenFileDialog.FileName
    '如果用户取消了,或没有选择任何文件,处理返回仍为数组形式,OpenFileFuction(0)为""
    If tempStr = "" Then
        ReDim tempFileName(0)
        tempFileName(0) = ""
        OpenFile = tempFileName
        Exit Function
    End If
    Dim P, N As Long
    P = 0                       '分隔字符位置
    N = 0                       '文件个数
    '提取路径
    ReDim tempFileName(1)                                      '至少有一个文件
    'P = InStr(1, tempStr, " ")                              '为UNICODE
    P = InStr(1, tempStr, Chr$(0))                  '为UNICODE
    Debug.Print tempStr & "   p=" & P

    If P = 0 Then                     '如果只选择了一个文件

        P = InStrRev(tempStr, "\")                          'P取得"\"所在位置
        tempFileName(0) = Left$(tempStr, P - 1)
        tempFileName(1) = Mid$(tempStr, P + 1)
        tempFileName(0) = IIf(Right$(tempFileName(0), 1) <> "\", tempFileName(0) & "\", tempFileName(0))
        '        tempFileName(0) = GetFilePath(tempStr)
        '        tempFileName(1) = GetFileName(tempStr)
    Else                                                         '选择了多个文件
        Do While tempStr <> ""
            P = InStr(tempStr, Chr$(0))                   '为UNICODE
            ReDim Preserve tempFileName(N)

            '如果没有空格(只剩余一个文件)
            If P = 0 Then
                tempFileName(N) = tempStr
                Exit Do
            Else
                tempFileName(N) = Left$(tempStr, P - 1)
                tempStr = Mid$(tempStr, P + 1)
                N = N + 1
            End If
        Loop
        tempFileName(0) = IIf(Right$(tempFileName(0), 1) <> "\", tempFileName(0) & "\", tempFileName(0))    '处理路径
    End If
    '返回文件名数组
    OpenFile = tempFileName
    '还原为单选
    OpenFileDialog.Flags = 0
    '------------------------------------------------
    '    Exit Function
    '    '----------------
ToExit:
    ErrorLog "modFileOption\OpenFile"
    Resume Next

End Function

 第二种方法==================================================

Public Function OpenFile(strFileInfor As String, strFileType As String, blnIsMulti As Boolean, OpenFileDialog As Control, StrIntdir As String) As String()
    On Error GoTo ToExit '打开错误陷阱
    '------------------------------------------------
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'OpenFileDialog         通用对话框
    'strstrfileInfor       文件提示
    'strstrfileType         文件类型,扩展名
    'blnblnismulti           是否为多选
    '返回为文件名数组,OpenFile(0)存放文件路径,从1开始存放文件名,包括只有一个文件时的情况。
    '返回的路径中已经包含了最后的"\"
    '如果没有选择任何文件,OpenFileFuction(0)返回为""
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '设置打开文件对话框过滤器

    OpenFileDialog.Filter = strFileInfor & "|" & strFileType
    '设定是否可以多选
    If blnIsMulti Then
        OpenFileDialog.Flags = &H80200
        OpenFileDialog.MaxFileSize = 1024         '设置使用CommonDialog控件被打开的文件名的最大尺寸
    Else
        OpenFileDialog.Flags = 0
    End If

    OpenFileDialog.InitDir = StrIntdir
    OpenFileDialog.FileName = ""
    OpenFileDialog.ShowOpen
    '提取各文件名
    Dim tempStr     As String
    Dim tempFileName()     As String
    tempStr = OpenFileDialog.FileName
    '如果用户取消了,或没有选择任何文件,处理返回仍为数组形式,OpenFileFuction(0)为""
    If tempStr = "" Then
        ReDim tempFileName(0)
        tempFileName(0) = ""
        OpenFile = tempFileName
        Exit Function
    End If
    Dim P, n As Long
    P = 0                       '分隔字符位置
    n = 0                       '文件个数
    '提取路径
    tempFileName = Split(tempStr, Chr$(0))
    If UBound(tempFileName) = 0 Then   '选择一个文件
        ReDim tempFileName(1)
        tempFileName(0) = GetFilePath(tempStr) '提取路径
        tempFileName(1) = GetFileName(tempStr) '取文件名
    Else
        选择多个文件

        tempFileName(0) = IIf(Right$(tempFileName(0), 1) <> "\&#

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值