HOWTO: Retrieve Multiple Filenames from Common Dialog ControlID: Q145612
|
The Common Dialog File control allows you to select one or more files to use in your Visual Basic application program. This article shows how to retrieve the names of the selected files from the Common Dialog control.
Visual Basic's Common Dialog File control allows you to provide access to
the directory and file structure of your hard disk from within an
application program. For example, if your user needs to select a text file,
you can display a Common Dialog File Control box and the user can traverse
the directories until he finds the individual file he wants to access.
By setting the Flags property of the Common Dialog control to the constant
value OFN_ALLOWMULTISELECT, your user may select several files to work
with. Multiple files can be selected by clicking on each filename while
holding down the SHIFT or CTRL key. The selected filenames are highlighted.
When your Visual Basic program needs to work with the files selected by the
user, you need to retrieve each individual filename from the control's
Filename property. The filenames selected by the user are all stored in
this property as one long string. Each filename is separated by a space
(32) character.
Therefore, to retrieve each individual filename from the Common Dialog's
Filename property, you use the InStr function to search for the delimiting
space character. The InStr function returns the location of the space
character within the Filename property string. Next, you use the Mid
function to remove the individual filename entry from the string.
Private Sub Command1_Click()
Dim I As Integer
Dim Y As Integer
Dim Z As Integer
Dim FileNames$()
CommonDialog1.filename = ""
CommonDialog1.Filter = "All Files|*.*"
CommonDialog1.Flags = cdlOFNAllowMultiselect
CommonDialog1.Action = 1
CommonDialog1.filename = CommonDialog1.filename & Chr(32)
Z = 1
For I = 1 To Len(CommonDialog1.filename)
I = InStr(Z, CommonDialog1.filename, Chr(32))
If I = 0 Then Exit For
ReDim Preserve FileNames(Y)
FileNames(Y) = Mid(CommonDialog1.filename, Z, I - Z)
Z = I + 1
Y = Y + 1
Next
If Y = 1 Then
Text1.Text = FileNames(0)
Else
Text2.Text = ""
For I = 0 To Y - 1
If I = 0 Then
Text1.Text = FileNames(I)
Else
Text2.Text = Text2.Text & UCase(FileNames(I)) & _
Chr$(13) & Chr$(10)
End If
Next
End If
End Sub
Product Documentation, Officer Developer's Kit, Visual Basic 3.0 Professional Edition. Common Dialog Control
Keywords : kbVBp400 kbVBp500 kbhowto PrgCtrls VB4WIN
Version : 4.0 5.0
Platform : NT WINDOWS
Issue type :
Last Reviewed: June 21, 1999