Using a FolderBrowserDialog
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
4 posts
Page 1 of 1
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
Why do you create a new array when the dialog already provides one? the same goes for the dialog result.
I believe this is how it should be.
I believe this is how it should be.
Code: Select all
Good tutorial though!.. Very well explained wahooo; Dim FBD As New FolderBrowserDialog
If FBD.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = FBD.SelectedPath
Dim FolderInfo As New IO.DirectoryInfo(FBD.SelectedPath)
For Each File As IO.FileInfo In FolderInfo.GetFiles("*.*")
ListBox1.Items.Add(File.Name)
Next
End If
You can find me on Facebook or on Skype mihai_92b
Thanks for suggesting improvements. Actually the thing is, I haven't used a FolderBrowserDialog much. Only in 4 of my apps, if I remember correctly.
I knew the dialog result could be done your way but I liked the other way around as both do the same thing. But I think using a control's own specific ways is always good.
I knew the dialog result could be done your way but I liked the other way around as both do the same thing. But I think using a control's own specific ways is always good.
Nice tutorial for beginners +rep 

My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023