Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Drive Box

0.00/5 (No votes)
4 Jul 2001 1  
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:\".

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.

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