Client Login Contact Us imi : home

Home

Clients
Services
Industries
Capabilities

News Releases
Articles
About imi
Why We're Here
Technology Partners
Join Our Team



by Caspar Boekhoudt

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-downlist, 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; }
}

How to use:

To use the FolderDialog, add the following code to your class. If the user hits the select button, it returns the folder path.

FolderDialog folderDialog = new FolderDialog();
if (folderDialog.ShowDialog()==DialogResult.OK)
{
txtInput.Text=folderDialog.Path;
}

About the Author:

Caspar Boekhoudt is a Consultant for Information Methodologies Inc. (imi), higher ed's leading enterprise web integrator. He has a M.S. in Computer Science and is a Sun Certified Programmer for the Java 2 Platform and IBM Certified Developer, XML and Related Technologies V1.

This article originally published at C# Corner.