Using a FolderBrowserDialog
Posted: Thu Apr 03, 2014 3:27 pm
A FolderBrowserDialog is used to select a directory. The directory can then be used as a destination for saving files or a source for loading files from.
In this tutorial, the basics of using such a dialog along with its various properties will be explained.
I assume that you have a TextBox and a Button on your form. The button will be used to open the dialog and the textbox is where the selected directory will appear.
To create a new instance of the FolderBrowserDialog:
The RootFolder property sets the directory where the browsing will start from when the dialog is opened. Environment.SpecialFolder is a collection of some commonly used directories.
Additional coding can be done to do something with the directory rather than keeping it safe in a TextBox. :P An example could be to load all files within the directory into a listbox. For this purpose, add a ListBox to your Form and add the following code within the "If... Then... End If":
In this tutorial, the basics of using such a dialog along with its various properties will be explained.
I assume that you have a TextBox and a Button on your form. The button will be used to open the dialog and the textbox is where the selected directory will appear.
To create a new instance of the FolderBrowserDialog:
Code: Select all
Now we'll set the various properties of the dialog...Dim FBD As New FolderBrowserDialog
The RootFolder property sets the directory where the browsing will start from when the dialog is opened. Environment.SpecialFolder is a collection of some commonly used directories.
Code: Select all
Description property determines the text that will appear on the top of the dialog. You should change this to something that would help the user decide which directory to select. For example, if the purpose of the dialog is to load images from a folder, following can be used:
FBD.RootFolder = Environment.SpecialFolder.Desktop
Code: Select all
To give the user the ability to create new folders while browsing directories, you should set this property to True.
FBD.Description = "Select a folder to load images from..."
Code: Select all
If you want the dialog to browse to the currently selected directory, the SelectedPath property can be used. There is no need to do a "Try... Catch... End Try" here as the dialog browses to the default directory if the provided directory didn't exist or was unavailable.
FBD.ShowNewFolderButton = True
Code: Select all
These are most commonly used properties. Now it's time to get the dialog to some work. First, we'll declare the dialog's result as 'rslt' and associate it with displaying the dialog.
FBD.SelectedPath = TextBox1.Text
Code: Select all
The above code will display the FolderBrowserDialog. To code what happens when the OK button is pressed on the dialog, we use the following code:
Dim rslt As DialogResult = FBD.ShowDialog
Code: Select all
It means that if the OK button is pressed, then set the TextBox's text to the dialog's selected directory. If rslt = Windows.Forms.DialogResult.OK Then
TextBox1.Text = FBD.SelectedPath
End If
Additional coding can be done to do something with the directory rather than keeping it safe in a TextBox. :P An example could be to load all files within the directory into a listbox. For this purpose, add a ListBox to your Form and add the following code within the "If... Then... End If":
Code: Select all
That's all for this tutorial. If you need help coding something using a FolderBrowserDialog, or any other dialog, or anything in general in VB.Net, then feel free to leave a comment.Dim FolderInfo As New IO.DirectoryInfo(FBD.SelectedPath)
Dim ArrayFiles() As IO.FileInfo = FolderInfo.GetFiles(".")
For Each File In ArrayFiles
ListBox1.Items.Add(File.Name)
Next