Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / Visual Basic
Article

Folder, Drive, Directory Browser

Rate me:
Please Sign up or sign in to vote.
2.58/5 (35 votes)
13 Sep 2004CPOL2 min read 122.5K   3.2K   33   6
Folder, drive, directory browser application.

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.

C#
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.

C#
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:

C#
private void Exit_Click(object sender, System.EventArgs e)
{
    this.Dispose();
}

For more help and queries, please mail me: vivekthangaswamy@rediffmail.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer at Zealots
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThis is very usefull for me Pin
xkermit21-Apr-07 1:58
xkermit21-Apr-07 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.