Disclaimer:
This source code is written and maintained by an individual, not a company. It has been provided for free to the Microsoft .NET user community with the expectation that users will take whatever action necessary to get the code to operate according to their needs. This includes debugging and enhancements. The source code is provided "as is" and there are neither warrantees to the quality of the work nor any expressed or implied agreements that the programmer will release any updates. The programmer will release updates and provide any support at his own discretion.
Introduction
The article is mainly for beginners of .NET WinForms programming. Recently, I heard that quite a few people have questions on developing the folder and file dialog boxes. Actually, it is a simple and easy one. This article is to give a simple example in VB.NET as well as in C# showing that how easy it is to create a custom drive list Box, directory list box and a file list box. This is developed using Visual Studio .NET final release.
Adding "DriveListBox", "DirListBox", and "FileListBox" to VS.NET Toolbox:
Select �Tools� menu and Select �Add/Remove Tool Box Items�. Then the �Customize Toolbox� will appear in the screen. Select �.NET Framework Components� tab and then select the �DriveListBox�, �DirListBox�, and �FileListBox� checkboxes and hit OK. Now you can drag and drop the "DriveListBox
", "DirListBox
", and "FileListBox
" into a Windows Form.
driveListBox_SelectedIndex Method:
When the user selects a drive from the DriveListBox
drop-down list, this method updates the path of the DirListBox
to be consistent with the DriveListBox
. If the drive is unavailable or missing or if there is some other problem, then it will show a message on the screen with the error alert and the DriveListBox
. The drive is then restored to its original selection.
private void driveListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
this.dirListBox1.Path = this.driveListBox1.Drive;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
dirListBox1_SelectedIndexChanged:
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 or missing or if there is some other problem then it will show a message on the screen with the error alert.
private void dirListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
this.fileListBox1.Path = this.dirListBox1.Path;
this.lblFolder.Text =
dirListBox1.Path.Substring(dirListBox1.Path.LastIndexOf("\\")+1);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
To exit from the application, use the following code:
private void Exit_Click(object sender, System.EventArgs e)
{
this.Dispose();
}
For more help and queries, please mail me: vivekthangaswamy@rediffmail.com.