SIGN UP MEMBER LOGIN:    
ARTICLE

Custom FileDialog

Posted by casper boekhoudt Articles | Windows Controls C# January 18, 2002
The purpose of this article is to give a simple example showing how easy it is to create a custom FolderDialog.
Reader Level:
Download Files:
 

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

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

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor