Click here to Skip to main content
12,635,139 members (25,833 online)
Click here to Skip to main content
Add your own
alternative version

Stats

89.3K views
880 downloads
22 bookmarked
Posted

Drive Box

, 4 Jul 2001
Rate this:
Please Sign up or sign in to vote.
This article explains how to display the list of Drive in a ComboList box.
<!-- Download Links --> <!-- Article image -->

Sample Image - DriveBox.jpg

<!-- Add the rest of your HTML here -->

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

Share

About the Author

Gobinath.K.R
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questiondrivebox Pin
Member 1006594920-May-13 13:22
memberMember 1006594920-May-13 13:22 
GeneralNice But ... Pin
Hamed_ji13-May-07 2:05
memberHamed_ji13-May-07 2:05 
GeneralImages in DriveBox Pin
Anonymous6-Jul-01 9:47
memberAnonymous6-Jul-01 9:47 
GeneralRe: Images in DriveBox Pin
Anonymous12-Jul-01 5:53
memberAnonymous12-Jul-01 5:53 
AnswerRe: Images in DriveBox Pin
Michael B. Hansen8-May-07 21:18
memberMichael B. Hansen8-May-07 21: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161208.2 | Last Updated 5 Jul 2001
Article Copyright 2001 by Gobinath.K.R
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid