What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleLet the user pick multiple files in VB .NET
DescriptionThis example shows how to let the user pick multiple files in VB .NET.
Keywordsfile, open file, select file, VB .NET
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.

When the program starts, it initializes arrays containing references to its TextBox and Button controls. It then loads the previously saved file names and the dialog filter numbers used the last time.

Private m_FilterSelected(MAX_CTL) As Integer

Private m_txtFile(MAX_CTL) As TextBox
Private m_btnBrowse(MAX_CTL) As Button

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make arrays referencing the TextBoxes and Buttons.
    m_txtFile(0) = txtFile0
    m_txtFile(1) = txtFile1
    m_txtFile(2) = txtFile2
    m_txtFile(3) = txtFile3

    m_btnBrowse(0) = btnBrowse0
    m_btnBrowse(1) = btnBrowse1
    m_btnBrowse(2) = btnBrowse2
    m_btnBrowse(3) = btnBrowse3

    ' Load previously saved file names and filter indices.
    For i As Integer = 0 To MAX_CTL
        m_txtFile(i).Text = GetSetting(APP_NAME, "Files", _
            "File" & i, "")
        m_FilterSelected(i) = _
            Integer.Parse(GetSetting(APP_NAME, "Files", _
            "Filter" & i, "0"))
    Next i
End Sub
When the program ends, the Unload event handler saves the current file names and the most recently used file filter numbers for each file.
Private Sub Form1_Closing(ByVal sender As Object, ByVal e _
    As System.ComponentModel.CancelEventArgs) Handles _
    For i As Integer = 0 To MAX_CTL
        SaveSetting(APP_NAME, "Files", "File" & i, _
        SaveSetting(APP_NAME, "Files", "Filter" & i, _
    Next i
End Sub
When the user clicks one of the form's browse buttons, the program gets the index of the button pressed from its Tag property (set at design time). It prepares the File Open dialog, sets its file name to the corresponding TextBox's current value, and displays the dialog. If the user clicks OK, the program saves the selected file name and the dialog's current filter number for use next time.
    If dlgFile.ShowDialog() = DialogResult.OK Then
        m_txtFile(index).Text = dlgFile.FileName
        m_FilterSelected(index) = dlgFile.FilterIndex
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.