HOWTO: Retrieve Multiple Filenames from Common Dialog Control

ID: Q145612


The information in this article applies to:


SUMMARY

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.


MORE INFORMATION

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.

Create the Demonstration Program

The demonstration program below shows how to retrieve all filenames selected in a Common Dialog File control.
  1. Create a new project in Visual Basic. Form1 is created by default.


  2. Add a Common Dialog control to Form1. CommonDialog1 is created by default.


  3. Add a Text Box control to Form1. Text1 is created by default.


  4. Add a second Text Box control to Form1. Text2 is created by default. Set its MultiLine property to True.


  5. Add a CommandButton control to Form1. Command1 is created by default.


  6. Add the following code to the Click event for Command1.
    
          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
     


Execute the demonstration program by pressing the F5 key. Click on the CommandButton. The Common Dialog Box File control is displayed on the screen. Select several files from the file list by holding down the SHIFT or CTRL key while clicking the mouse on a filename. When you have selected several files, click OK. The filenames are displayed in the second Text Box control and the directory name is displayed in the first Text Box control.


REFERENCES

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