VB.Net - OpenFileDialog Control

The OpenFileDialog control prompts the user to open a file and allows the user to select a file to open. The user can check if the file exists and then open it. The OpenFileDialog control class inherits from the abstract class FileDialog.
If the ShowReadOnly property is set to True, then a read-only check box appears in the dialog box. You can also set the ReadOnlyChecked property to True, so that the read-only check box appears checked.
Following is the Open File dialog box −
The following are some of the commonly used properties of the OpenFileDialog control −
AddExtension
Gets or sets a value indicating whether the dialog box automatically adds an extension to a file name if the user omits the extension.
AutoUpgradeEnabled
Gets or sets a value indicating whether this FileDialog instance should automatically upgrade appearance and behavior when running on Windows Vista.
CheckFileExists
Gets or sets a value indicating whether the dialog box displays a warning if the user specifies a file name that does not exist.
CheckPathExists
Gets or sets a value indicating whether the dialog box displays a warning if the user specifies a path that does not exist.
CustomPlaces
Gets the custom places collection for this FileDialog instance.
DefaultExt
Gets or sets the default file name extension.
DereferenceLinks
Gets or sets a value indicating whether the dialog box returns the location of the file referenced by the shortcut or whether it returns the location of the shortcut (.lnk).
FileName
Gets or sets a string containing the file name selected in the file dialog box.
FileNames
Gets the file names of all selected files in the dialog box.
Filter
Gets or sets the current file name filter string, which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.
FilterIndex
Gets or sets the index of the filter currently selected in the file dialog box.
InitialDirectory
Gets or sets the initial directory displayed by the file dialog box.
Multiselect
Gets or sets a value indicating whether the dialog box allows multiple files to be selected.
ReadOnlyChecked
Gets or sets a value indicating whether the read-only check box is selected.
RestoreDirectory
Gets or sets a value indicating whether the dialog box restores the current directory before closing.
SafeFileName
Gets the file name and extension for the file selected in the dialog box. The file name does not include the path.
SafeFileNames
Gets an array of file names and extensions for all the selected files in the dialog box. The file names do not include the path.
ShowHelp
Gets or sets a value indicating whether the Help button is displayed in the file dialog box.
ShowReadOnly
Gets or sets a value indicating whether the dialog box contains a read-only check box.
SupportMultiDottedExtensions
Gets or sets whether the dialog box supports displaying and saving files that have multiple file name extensions.
Title
Gets or sets the file dialog box title.
ValidateNames
Gets or sets a value indicating whether the dialog box accepts only valid Win32 file names.
The following are some of the commonly used methods of the OpenFileDialog control −
OpenFile
Opens the file selected by the user, with read-only permission. The file is specified by the FileName property.
Reset
Resets all options to their default value.
In this example, let's load an image file in a picture box, using the open file dialog box. Take the following steps −
Drag and drop a PictureBox control, a Button control and a OpenFileDialog control on the form.
Drag and drop a PictureBox control, a Button control and a OpenFileDialog control on the form.
Set the Text property of the button control to 'Load Image File'.
Set the Text property of the button control to 'Load Image File'.
Double-click the Load Image File button and modify the code of the Click event:

Double-click the Load Image File button and modify the code of the Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
   End If
End Sub
When the application is compiled and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −
Click on the Load Image File button to load an image stored in your computer.

    63 Lectures 
     4 hours 

 Frahaan Hussain

    103 Lectures 
     12 hours 

 Arnold Higuit

    60 Lectures 
     9.5 hours 

 Arnold Higuit

    97 Lectures 
     9 hours 

 Arnold Higuit
 Print
 Add Notes
 Bookmark this page