BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.NET 1.1+

The Folder Browser Dialog Box

The .NET framework provides a number of commonly used dialog boxes. These are designed to allow familiar methods for user input. They include font, file and colour selection and printer configuration dialogs. This article describes the folder browser.

Adding a Description

The folder browser dialog includes a blank area between the title bar and the tree view. This area can be used to display a description or an instruction to the user. This must be plain text, set using the Description property.

To demonstrate, modify the method that displays the dialog box, as follows:

private void SelectFolderButton_Click(object sender, EventArgs e)
{
    using (FolderBrowserDialog dialog = new FolderBrowserDialog())
    {
        dialog.Description = "Choose the folder for temporary files.";

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            FolderDetails.Text = dialog.SelectedPath;
        }
    }
}

The updated window appears as shown below:

Folder Browser Dialog Box with Description

Setting the Root Folder

Depending upon the type of software that you develop, you might want to limit the folders from which the user may select. For example, if you are writing video editing software, you might elect to start the dialog box with the "My Videos" folder as the root of the tree. You can select from a number of root folders using the RootFolder property. This should be set to a value from the Environment.SpecialFolder enumeration before displaying the dialog box. Once set, the user is prevented from browsing for folders that appear higher in the hierarchy.

The following example sets the root folder for the tree to the user's "My Documents" folder. The actual name displayed for this folder varies according to the operating system version.

private void SelectFolderButton_Click(object sender, EventArgs e)
{
    using (FolderBrowserDialog dialog = new FolderBrowserDialog())
    {
        dialog.Description = "Choose the folder for temporary files.";

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            FolderDetails.Text = dialog.SelectedPath;
            dialog.RootFolder = Environment.SpecialFolder.MyDocuments;
        }
    }
}

Preventing the User from Creating Folders

By default, the folder browser includes a Make New Folder button. Clicking this button creates a new folder beneath the selected item and allows it to be renamed. In some, limited scenarios, it may be useful to remove the button. You can do so by setting the ShowNewFolderButton property to false.

private void SelectFolderButton_Click(object sender, EventArgs e)
{
    using (FolderBrowserDialog dialog = new FolderBrowserDialog())
    {
        dialog.Description = "Choose the folder for temporary files.";
        dialog.RootFolder = Environment.SpecialFolder.MyDocuments;
        dialog.ShowNewFolderButton = false;

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            FolderDetails.Text = dialog.SelectedPath;
        }
    }
}
15 December 2013