Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLet the user pick multiple files
DescriptionThis example shows how to let the user pick multiple files in Visual Basic 6.
Keywordsfile, open file, select file
CategoriesSoftware Engineering, Files and Directories
 
When a program lets the user pick more than one file, there's always a question about how to handle the current directory. Should the program update the current directory each time the user picks a file? Should different file selections have separate current directories? For example, if you're loading a file, transforming it, and saving it in a new file, should the program track separate directories for the old and new files?

The program lets the user load several files and keeps track of the directories for each. It also keeps track of the file selection dialog filter the user picks for each.

The program has control arrays to hold the file names and buttons that let the user browse for a file. When the user clicks one of the browse buttons, the cmdBrowse_Click event handler executes. It prepares a common dialog control (the program uses one of these for all of the files) and sets the control's FileName property to the file's current value. That sets the dialog's current directory to the file's last location. The program displays the dialog and, if the user accepts, shows the result in the proper TextBox.

The program's Form_Load event handler loads the previously saved file values from the registry and the Form_Unload event handler saves the current values for next time.

 
Private m_FilterSelected() As Integer

Private Sub cmdBrowse_Click(Index As Integer)
    dlgFile.Flags = cdlOFNFileMustExist Or _
        cdlOFNHideReadOnly
    dlgFile.CancelError = True
    dlgFile.Filter = "Text Files (*.txt)|*.txt|VB 6 Files " & _
        "(*.frm, *.frx, *.vbp, *.vbw, *.bas, " & _
        "*.cls)|*.frm;*.frx;*.vbp;*.vbw;*.bas;*.cls|All " & _
        "Files (*.*)|*.*"
    dlgFile.FilterIndex = m_FilterSelected(Index)
    dlgFile.FileName = txtFile(Index).Text

    On Error Resume Next
    dlgFile.ShowOpen

    If Err.Number = cdlCancel Then Exit Sub
    If Err.Number <> 0 Then
        MsgBox "Error " & Err.Number & " selecting " & _
            "database." & _
            vbCrLf & Err.Description
        Exit Sub
    End If
    On Error GoTo 0

    txtFile(Index).Text = dlgFile.FileName
    m_FilterSelected(Index) = dlgFile.FilterIndex
End Sub

Private Sub Form_Load()
Dim i As Integer

    ReDim m_FilterSelected(txtFile.LBound To txtFile.UBound)

    For i = txtFile.LBound To txtFile.UBound
        txtFile(i).Text = GetSetting(APP_NAME, "Files", _
            "File" & i, "")
        m_FilterSelected(i) = GetSetting(APP_NAME, "Files", _
            "Filter" & i, "0")
    Next i
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer

    For i = txtFile.LBound To txtFile.UBound
        SaveSetting APP_NAME, "Files", "File" & i, _
            txtFile(i).Text
        SaveSetting APP_NAME, "Files", "Filter" & i, _
            m_FilterSelected(i)
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated