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
Contributors
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Using a FolderBrowserDialog
Usman55
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:
Code: Select all
Dim FBD As New FolderBrowserDialog
Now we'll set the various properties of the dialog...

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
FBD.RootFolder = Environment.SpecialFolder.Desktop
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:
Code: Select all
FBD.Description = "Select a folder to load images from..."
To give the user the ability to create new folders while browsing directories, you should set this property to True.
Code: Select all
FBD.ShowNewFolderButton = True
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.
Code: Select all
FBD.SelectedPath = TextBox1.Text
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.
Code: Select all
Dim rslt As DialogResult = FBD.ShowDialog
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:
Code: Select all
If rslt = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = FBD.SelectedPath
        End If
It means that if the OK button is pressed, then set the TextBox's text to the dialog's selected directory.

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
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
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.
Image
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: Using a FolderBrowserDialog
XTechVB
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.
Code: Select all
        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
Good tutorial though!.. Very well explained wahooo;
You can find me on Facebook or on Skype mihai_92b
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: Using a FolderBrowserDialog
Usman55
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.
Image
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Using a FolderBrowserDialog
smashapps
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
Return to “Tutorials”