Introduction
It has recently come to my attention that there are quite a few people that have questions on how to develop a FolderDialog or how to add a directory list to a Windows Form.
The purpose of this article is to give a simple example showing how easy it is to create a custom FolderDialog. This class was developed using VS.NET final release.
Adding "DriveListBox","DirListBox", and "FileListBox" to VS.NET Toolbox:
Right Click "Toolbox"
select "Customize Toolbox"
Select the ".NET Framework Component" tab
Select the "DriveListBox","DirListBox", and "FileListBox" checkboxes and hit OK
Now you can drag and drop the "DriveListBox","DirListBox", and "FileListBox" into the Windows Form.
driveListBox_SelectedIndex Method:
When the user selects a drive from the driveListBox drop-downl list, this method updates the path of the dirListBox to be consistent with the driveListBox. If the drive is unavailable, a MessageBox is presented to the user with the error, and the driveListBox.Drive is restored to its original selection.
private
void driveListBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
this.dirListBox.Path = this.driveListBox.Drive;
tempDrive= this.driveListBox.Drive;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
driveListBox.Drive = tempDrive;
}
}
dirListBox_Change Method:
When the user double-clicks on a folder, or the dirListBox is updated because the drive was changed, this method updates the Path of the fileListBox to be consistent with the dirListBox. If the directory is unavailable, a MessageBox is presented to the user.
private
void dirListBox_Change(object sender, System.EventArgs e)
{
try
{
this.fileListBox.Path = this.dirListBox.Path;
this.lblFolder.Text = dirListBox.Path.Substring(dirListBox.Path.LastIndexOf("\\")+1);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
Path Property:
The directory path selected
public string Path
{
get { return dirListBox.Path; }
}