OpenFileDialog in Visual Basic .NET

Last Updated: March 19, 2020

OpenFileDialog is one of the dialog control in VB.NET. You can use this control to open the file and get its filename
You can use filtering to show the files you want. There are several properties of the OpenFileDialog control which you can change according to your requirement

Now I am going to code the following user interface to open the FileDialog and get the name of the file you select to TextBox control

First you have to frag and drop the OpenFileDialog control from the ToolBox


This is the code for the Button Click handler

Public Class Form1

    Private Sub ButtonOpen_Click(sender As Object, e As EventArgs) Handles ButtonOpen.Click
        OpenFileDialog1.Title = "Open File Dialog Demo"
        OpenFileDialog1.Filter = "Text Dccoments (*.txt)|*.txt"

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            'get the file name to textbox
            TextBoxFileName.Text = OpenFileDialog1.FileName

        End If

    End Sub
End Class

Code explanation

  • Line 5: Using OpenFileDialog1.Filter property to filter only the text file
  • Line 7: When you open the OpenFileDialog window you can check for OK button press with Windows.Forms.DialogResult.OK
  • Line 9: Get the file name to TextBoxFileName.Text