CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2001
    Location
    Ontario, Canada
    Posts
    4

    Common Dialog Control: Huh?

    Greetings,

    I'd like to be able to select multiple files with my open common dialog control in my app, but when using the cdlOFNAllowMultiselect

    flag, the reference states:

    "...the FileName property returns a string containing the names of all selected files. The names in the string are delimited by spaces"

    Huh? Spaces? Why the heck would Microsoft seperate filenames with spaces when clearly any number of spaces can be found within filenames themselves?

    Is there an easy way of figuring out how many files and extracting them from the FileName property? A filename count perhaps? Or is there possibly a newer Common Dialog control (or if someone made one) that delmits multiple filenames with a different, more suitable character?

    Thanks,
    Aienthiwan


  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Common Dialog Control: Huh?

    If you use the cdlOFNAllowMultiSelect flag only, you will get the old (3.1) style open dialog that supports only 8.3 format filenames, so they are separated by spaces, no problem.

    But in your application, you will invariably use the cdlOFNExplorer flag too. And in this case, the filenames, though they are shown delimited by double quotes with spaces in between, are returned delimited by the NULL character,a nd this causes problems.

    Here is a code (adapted from MSDN) that will separate the names for you:


    option Explicit

    private Sub Command1_Click()
    Dim s as string, FileArray() as string
    Dim MyString as string, Path as string, Filename as string
    Dim J as Integer, I as Integer

    ReDim FileArray(1)
    CommonDialog1.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer
    CommonDialog1.ShowOpen
    s = Chr(0)
    MyString = CommonDialog1.Filename

    If InStr(1, MyString, s) > 0 then
    If (InStr(MyString, "\") + 1) = InStr(MyString, s) then
    Path = Trim(Left$(MyString, InStr(1, MyString, s) - 1))
    else
    Path = Trim(Left$(MyString, InStr(1, MyString, s) - 1)) & "\"
    End If
    Filename = Right$(MyString, len(MyString) _
    - InStr(1, MyString, s))
    MyString = Filename
    J = 0

    for I = 1 to len(Filename)
    If mid$(Filename, I, 1) = s then
    J = J + 1
    End If
    next
    J = J + 1
    ReDim FileArray(1 to J) as string
    for I = 1 to UBound(FileArray)
    If I < UBound(FileArray) then
    Filename = Trim(Left$(MyString, InStr(1, MyString, s) - 1))
    MyString = Trim(Right$(MyString, len(MyString) - InStr(1, MyString, s)))
    else
    Filename = MyString
    End If
    FileArray(I) = Filename
    MsgBox Filename
    next
    else
    for I = 1 to len(MyString)
    If mid$(MyString, I, 1) = "\" then
    Path = Left$(MyString, I)
    Filename = Right$(MyString, len(MyString) - I)
    End If
    next
    FileArray(UBound(FileArray)) = Filename
    End If

    End Sub







  3. #3
    Join Date
    Apr 2001
    Location
    Ontario, Canada
    Posts
    4

    Re: Common Dialog Control: Huh?

    Excellent!

    Thank you, you hit the problem right on the head.

    Aienthiwan


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured