Click here to Skip to main content
Click here to Skip to main content
Go to top

Folder, Drive, Directory Browser

, 13 Sep 2004
Rate this:
Please Sign up or sign in to vote.
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.

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.

License

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

Share

About the Author

vivekthangaswamy
Software Developer (Senior) Swordface
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralThanks PinmemberJohnAnas24-Feb-11 2:06 
GeneralMy vote of 4 Pinmemberaashish7729-Sep-10 6:00 
GeneraldirListBox1_Changed PinmemberHunB23-May-08 0:19 
GeneralThis is very usefull for me Pinmemberxkermit21-Apr-07 1:58 
GeneralTHX PinmemberHighmaster21-Feb-05 13:24 
GeneralRe: THX Pinmembervivekthangaswamy24-Feb-05 2:33 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 14 Sep 2004
Article Copyright 2004 by vivekthangaswamy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid