|A Drive List Control|
|Written by Ian Elliot|
|Friday, 09 October 2009|
Page 1 of 3
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.
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(
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:
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.
A plain vanilla drive list
|Last Updated ( Friday, 09 October 2009 )|