Windows Forms FolderBrowserDialog Component in VB.NET

The Windows Forms FolderBrowserDialog component is a modal dialog box that is used for browsing and selecting folders. New folders can also be created from within the FolderBrowserDialog component.
  • 2916
Download Files:
 

Introduction to the Windows Forms FolderBrowserDialog Component

The Windows Forms FolderBrowserDialog component is a modal dialog box that is used for browsing and selecting folders. New folders can also be created from within the FolderBrowserDialog component.

Note   To select files, instead of folders, use the OpenFileDialog component.

The FolderBrowserDialog component is displayed at run time using the ShowDialog method. Set the RootFolder property to determine the top-most folder and any subfolders that will appear within the tree view of the dialog box. Once the dialog box has been shown, you can use the SelectedPath property to get the path of the folder that was selected.

When it is added to a form, the FolderBrowserDialog component appears in the tray at the bottom of the Windows Forms Designer.

Choosing Folders with the Windows Forms FolderBrowserDialog Component

Often, within Windows applications you create, you will have to prompt users to select a folder, most frequently to save a set of files. The Windows Forms FolderBrowserDialog component allows you to easily accomplish this task.

To choose folders with the FolderBrowserDialog component

  1. In a procedure, check the FolderBrowserDialog component's DialogResult property to see how the dialog box was closed and get the value of the FolderBrowserDialog component's SelectedPath property.

  2. If you need to set the top-most folder that will appear within the tree view of the dialog box, set the RootFolder property, which takes a member of the SpecialFolder enumeration.

  3. Additionally, you can set the Description property, which specifies the text string that appears at the top of the folder-browser tree view.

    In the example below, the FolderBrowserDialog component is used to select a folder, similar to when you create a project in Visual Studio and are prompted to select a folder to save it in. In this example, the folder name is then displayed in a TextBox control on the form. It is a good idea to place the location in an editable area, such as a TextBox control, so that users may edit their selection in case of error or other issues. This example assumes a form with a FolderBrowserDialog component and a TextBox control.

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                TextBox1.Text = FolderBrowserDialog1.SelectedPath
            End If
        End Sub

    Output:

    FolderBrowse.gif

    Figure 1

    Security Note   To use this class, your assembly requires a privilege level granted by the FileIOPermissionAttribute.PathDiscoveryProperty property, which is part of the FileIOPermissionAccess enumeration. If you are running in a partial-trust context, the process might throw an exception because of insufficient privileges. For more information, see Code Access Security Basics.

Source Code

Source code is attach in the top of the article.

For information on how to save files, see Saving Files Using the SaveFileDialog Component.

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.