The Browse For Folder Dialog Box
The Save and Open dialog boxes allow a user to save or open files only. Microsoft Windows provides a dialog box specially made so that a user can select a…
Description#
While Save and Open dialogs are restricted to file operations, Microsoft Windows also includes a dedicated dialog for selecting entire directories. This is useful whenever an application requires a folder path for its operations. The dialog looks like this:
When this dialog box comes up, it displays the Desktop folder as the parent and all the other folders can be located from it. To use it, the user can click one of the folders or drives and click OK. If the desired folder is not seen but is available, the user can expand the existing folders and drives, click the desired folder, and click OK. If the necessary folder is not available at all, the user can first select an existing folder or drive, click the Make New Folder button, type a name for the new folder, and click OK. Besides the folders, the Browse For Folder dialog box also allows the user to navigate to folders or directories of the network.
Creating a Browse For Folder Dialog Box#
The Browse For Folder dialog box is made available through the FolderBrowserDialog class that is derived from the CommonDialog class. To provide its functionality to your application, at design time, from the Toolbox, click FolderBrowserDialog and click the form.
You can trigger this dialog in your code by declaring a FolderBrowserDialog variable and initializing it with the new keyword. To show the Browse For Folder window to the user, simply execute its ShowDialog() method. For example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
}
}
}This would produce:
Home Page Copyright © 2008-2009 C# Key Next

