- Published on : 15 December 2013 at 13:53 UTC
- Lionel Eppe
It's always interesting to allow the user to navigate on your hard drive when should specify the pathname of a file.
For this, just use the component "OpenFileDialog" provided in ".Net Framework".
For this tutorial, you will need :
1) a "TextBox" with such property :
- Name : TB_file_path
2) a "ListBox" with such property :
- Name : LBOX_files_paths
3) a "ListBox" with such property :
- Name : LBOX_files_names
4) a "Button" with such property :
- Name : BT_file_browse
5) a "OpenFileDialog" with such property :
- Name : OpenFileDialog1 (default name)
The component "OpenFileDialog" is quite comprehensive and allows you to customize it by changing the title of the window, filter the files displayed according to their extensions, ...
- Verify the existence of the selected file
- Set a default file extension
- Specify the file name that the user must select
- Filter displayed files according to their extensions
- Set the default folder
- Enable or disable multiple files selection
- Change the title of the window
- Recover or names / paths of the selected files (+ sources)
This feature is enabled by default by the properties "CheckFileExists" and "CheckPathExists" which will verify that the file and the file path exist.
You can set a default extension to be added after the name of the user-specified file (if it fails to indicate the file extension).
This property is useful when you expect to select a specific file. For example, when the user can select a configuration file with a specific name.
To do this, enter the file name in the property "FileName".
To filter the list of displayed files, simply specify a filter by the property "Filter". It's this system that is used by the majority of those programs whose image editing, web pages editors, ...
Here is an example that displays jpg images, png images, gif images or all files according to the user's choice.
OpenFileDialog1.Filter = "JPEG images|*.jpg|PNG images|*.png|Animated images|*.gif|All files|*.*"
Thus, the following code will allow the user to view a particular image format or all files in the folder where it's located.
It's possible to set the default folder in which the user will be at the opening of the selection window via the property "InitialDirectory".
If the folder does not exist, the default folder is "My Documents" folder.
You can enable or disable multiple file selection but if you enable it, this means that a array will be returned by this component instead of a simple string. The recovery of this information or these will not do so in the same way.
To enable or disable this feature, change the "MultiSelect" property.
To change the title of the window to select a file or files, you will need as you guessed it, change the property "Title".
As the title of this section tells you, two scenarios arise :
- Either the multiple selection is disabled, and it will simply retrieve the path of the selected file
- Either the multiple selection is enabled, in which case it will retrieve an array of files and therefore go through it with a loop.
Start with the first case that is easier to understand :
' It displays the form and tests whether the user has selected a file. ' The user will have clicked on the OK button. If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' Gets the full path of the file selected by the user TB_file_path.Text = OpenFileDialog1.FileName ' Displays the file name (only) selected, to the user MsgBox("You have selected the file : " & OpenFileDialog1.SafeFileName) Else ' If the user does not select a file, we show a warning MsgBox("No file was selected", MsgBoxStyle.Exclamation, "No file selected") End If
For the second case, we use a loop "For Each" that is most appropriate to browse this type of array.
' We display the form and we check if the user has selected a file. ' The user will have clicked on the OK button. If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' We empty lists (ListBox) LBOX_files_paths.Items.Clear() LBOX_files_names.Items.Clear() ' We traverses the list of user-selected files For Each file_path As String In OpenFileDialog1.SafeFileNames ' And we add their paths in the list (ListBox) LBOX_files_paths.Items.Add(file_path) Next ' We re-traverses the list of user-selected files (contains files names only) For Each file_name As String In OpenFileDialog1.FileNames ' And add their names in the list (ListBox) LBOX_files_names.Items.Add(file_name) Next Else ' If the user does not select a file, we display a warning. MsgBox("No file was selected", MsgBoxStyle.Exclamation, "No file selected") End If