Click here to Skip to main content
15,860,844 members
Articles / Programming Languages / C#
Article

Drive Box

Rate me:
Please Sign up or sign in to vote.
2.56/5 (6 votes)
4 Jul 20012 min read 97.9K   982   22   5
This article explains how to display the list of Drive in a ComboList box.

Sample Image - DriveBox.jpg

Introduction

A DriveBox control enables a user to select a valid disk drive at run time. This control can be used to display a list of all the valid drives in a user's system. You can create dialog boxes that enable the user to open a file from a list of files on a disk in any available drive. This control is very similar to DriveListBox Control which we are very familiar.

Getting Started

For the DriveBox to display the valid disk drive, we require to find out the available drives in the user's system. Once the available drives information is obtained it can be displayed in a ComboBox.

Directory Class

To obtain the available drives, .Net framework provides us with the class Directory. This class is defined in System.IO namespace. This class exposes routines for getting local drives, creating, moving, and enumerating through directories and subdirectories. The method of our interest is Directory.GetLogicalDrives Method. The signature of this method is

public static string[] GetLogicalDrives();

This method retrieves the names of the logical drives on this machine in the form "C:\".

C#
namespace MyControls {
using System;
using System.IO;
using System.WinForms;

public sealed class DriveBox : ComboBox{
    DriveBox(){
        lstCollect = new ObjectCollection(this);
        Style = ComboBoxStyle.DropDownList;
        foreach (String str in Directory.GetLogicalDrives()){    
            str = str.Replace('\\',' ');
            lstCollect.Add(str);
        }
        SelectedIndex = 0;
    }
    public String Drive{
        get{
            return Text;
        }
    }
}
}

The above code defines class DriveBox. DriveBox is inherited from ComboBox and hence possess all the properties of ComboBox. The DriveBox is set to Dropdown List style. A new Property Drive is added which returns the currently selected Drive.

Compile the program using the following complier option.

csc filename.cs /out:MyControls.dll /target:library

Test Application

Now let us try to use the DriveBox in an small Test application. This test application will display all the available drives in the user's system. When the user Selects a drive in the DriveBox the Application displays a message box Prompting the currently selected Drive.

C#
using System;
using System.WinForms;
using System.Drawing;
using MyControls;
    
public class DriveBoxTest: Form{
    private Button btnOK = new Button();
    private DriveBox dbxLocal = new DriveBox();

    public static int Main(string[] args){
        Application.Run(new DriveBoxTest());
                return 0;
    }
        public DriveBoxTest(){
        this.Text = "DriveBox Test Program";

        dbxLocal.Location = new Point(1,10);
        dbxLocal.Size = new Size(80,20);

        btnOK.Location = new Point(90,100);
        btnOK.Size = new Size(80,20);
        btnOK.Text = "OK";

        this.Controls.Add(btnOK);
        this.Controls.Add(dbxLocal);
    
        btnOK.Click += new System.EventHandler(btnOK_Click);
        dbxLocal.SelectedIndexChanged += 
                   new System.EventHandler(dbxLocal_SelectedIndexChanged);
    }
    private void btnOK_Click(object sender, EventArgs evArgs){
        Application.Exit();
    }
    private void dbxLocal_SelectedIndexChanged(object sender,
                                               EventArgs evArgs){
        MessageBox.Show(dbxLocal.Drive,"DriveBox");
    }
}

Compile the program using the following complier option.

csc filename.cs /R:System.dll /R:System.WinForms.dll /R:System.Drawing.dll /R:MyControls.dll 

The output of the application will be as shown in the Sample Image.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiondrivebox Pin
Member 1006594920-May-13 12:22
Member 1006594920-May-13 12:22 
GeneralNice But ... Pin
Hamed J.I13-May-07 1:05
Hamed J.I13-May-07 1:05 
GeneralImages in DriveBox Pin
6-Jul-01 8:47
suss6-Jul-01 8:47 
GeneralRe: Images in DriveBox Pin
12-Jul-01 4:53
suss12-Jul-01 4:53 
AnswerRe: Images in DriveBox Pin
Michael B. Hansen8-May-07 20:18
Michael B. Hansen8-May-07 20:18 

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.