A Drive List Control
Written by Ian Elliot   
Friday, 09 October 2009
Article Index
A Drive List Control
OwnerDraw comboBox
A Drive Combo control

 

In this project we will recreate an apparently missing VB 6 control – the DriveCombo box – using nothing but a standard and not very difficult .NET forms approach. Microsoft have already done the job for us once in the form of the DriveListBox class within the VisualBasic.Compatibility.VB6 assembly. If you want an off-the-shelf solution then you have to include the entire VB DLL in your application.

The DriveCombo box is a useful control because it provides the user with an easy way to select a drive from a drop down list complete with icons that indicate the drive type.

As well as being useful in its own right it also serves as a blueprint for constructing more sophisticated combo boxes that combine text and raphics.

Basic comboBox

A comboBox is very easy to use. You place it on a form and then add to its Items collection anything that you want to appear in its dropdown list. You can add general objects to the Items collection and the comboBox will display their Text properties – this is an important detail as we shall see.

What we actually want to do is create a new control based on the comboBox but it is easier to see how the modifications work on a comboBox before turning it into a new control.

Start a new project called Combo1 and add the ShellIcons class developed in a previous project (change its namespaceand to Combo1) and a comboBox.

The first thing we need to do is populate the comboBox with a list of the available drives. This can be done in the Form Load event handler:

private void Form1_Load(
object sender, EventArgs e)
{
DriveInfo[] Drives =
DriveInfo.GetDrives();
foreach (DriveInfo Drive in Drives)
{
comboBox1.Items.Add(Drive);
}
comboBox1.SelectedIndex = 0;
}

The DriveInfo class can tell you everything you need to know about a drive. Its static GetDrives method returns an array of DriveInfo objects – one for each drive in the system.

Once we have this array we can step through it adding each DriveInfo object to the comboBox’s Items collection. When the comboBox displays its list it uses each DriveInfo’s text property.

To use the DriveInfo class we need to add:

using System.IO;

If you run this program as it stands you will see a comboBox preloaded with drive letters. This could be all you need but it looks distinctly unimpressive and drive icons would improve its usability.

 

combo1

A plain vanilla drive list

<ASIN:1904811604>
<ASIN:0321116208>

<ASIN:1932394656>



Last Updated ( Friday, 09 October 2009 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.