Click here to Skip to main content
15,843,498 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am still trying to display my combobox drive in the format of IconImage,VolumeName,VolumeLabel (Example) (Image LocalDisk C:\)

This is the code so far (with changes: IsReady and Try-Catch-End Try code block):
Imports System.IO
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Windows.Forms.Form
Imports System

Public Class Form1
    Public Sub New()
        Call InitializeComponent()
            If LicenseManager.UsageMode <> LicenseUsageMode.Designtime Then
                For Each d As DriveInfo In DriveInfo.GetDrives()
                    If d.IsReady Then                    
                        ComboBox1.Items.Add(d.Name &  d.VolumeLabel) 'never use + to join strings!
                    End If
                Next d
                ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
                ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        End If
        Catch ex As IO.IOException
            'MsgBox (ex.Message, MsgBoxStyle.Exclamation,"IOException")
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.SelectedIndex = 0
    End Sub

    Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
        If e.Index = -1 Then
        End If
        Dim drive As DriveInfo = CType(ComboBox1.Items(e.Index), DriveInfo)
        Dim icon As Icon
        Select Case drive.DriveType
            Case (DriveType.Fixed)
                icon = ShellIcons.HardDisk
            Case (DriveType.CDRom)
                icon = ShellIcons.CDROM
            Case (DriveType.Network)
                icon = ShellIcons.NetDisk
            Case (DriveType.Removable)
                icon = ShellIcons.FloppyDisk
            Case Else
                icon = ShellIcons.UnknownDisk
        End Select
        Dim iconbox As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height)
        e.Graphics.DrawIcon(icon, iconbox)
        Dim text As String = drive.Name
        If drive.IsReady Then
            If drive.VolumeLabel <> "" Then
                text = text & " (" & drive.VolumeLabel & ")"
            End If
        End If
        e.Graphics.DrawString(text, e.Font, New SolidBrush(e.ForeColor), e.Bounds.X + iconbox.Width + 1, e.Bounds.Y)
        If e.State = DrawItemState.Focus Then
        End If
    End Sub
End Class

I feel that this code is the closest I have come because when I try to debug it gives the error that the device is not ready which I suspect is the CDROM after placing the code in a try catch statement the C:\ And D:\ show but not CDROM and only the drive letters. Can Someone tell me where this code is wrong?

Thank you!
Updated 10-Jul-11 1:38am
Sergey Alexandrovich Kryukov 9-Jul-11 22:45pm    
I added PRE tags to render code sample properties and removed "Plain text only. Ignore HTML tags and entities" check box. Next time do it yourself; be aware of angular brackets, replace them with HTML entities &lt; and &gt;
Dale 2012 10-Jul-11 0:43am    
Ok thank you..... do you have any idea as to why I am getting the error Device is not ready? what can i do to fix it? Im stumped
Dale 2012 10-Jul-11 0:58am    
I have been working on this literally all day because originally I had been using SHcombobox in place of this code but cannot afford to purchase it. Please help
Dale 2012 10-Jul-11 1:06am    
I have made changes to the area Public Sub new to try to display the volume name but it errors "Device Not Ready" Sorry if re-posting this seems like spam. I honestly am only looking for help in this matter and not to make anyone mad or upset.

thank you

1 solution

You need to catch error: "Device not ready". Use Try.. Catch... Finally... End Try code block.
Try...Catch...Finally Statements[^][^]
Stay at Home and Learn
Try … Catch in VB .NET
Share this answer
Dale 2012 10-Jul-11 5:38am    
Ok I have made adjustments to the code but it still gives the error "Device Not Ready"

Public Sub New()
Call InitializeComponent()
If LicenseManager.UsageMode <> LicenseUsageMode.Designtime Then
For Each d As DriveInfo In DriveInfo.GetDrives()
ComboBox1.Items.Add(d.Name + d.VolumeLabel)
Next d
Catch ex As InvalidOperationException
End Try
ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
End If
End Sub
Dale 2012 10-Jul-11 5:42am    
If I change .... ComboBox1.Items.Add(d.Name + d.VolumeLabel) to
ComboBox1.Items.Add(d) it will once again work but only show the icon and driveinfo which is Icon C:\ and Icon D:\ and Icon E:\ but still no volumelabel
Maciej Los 10-Jul-11 6:02am    
On msdn-driveinfo site you'll find in remarks section: Use the IsReady property to test whether a drive is ready because using this method on a drive that is not ready will throw a IOException.
Dale 2012 10-Jul-11 6:04am    
is there a problem with the private sub draw item maybe somewhere?....
I have seen more than 60 websites with similar examples and have even made an application that displays the volumelabel in a msgbox but when I try to make the code example work in my code it loads blank and then shuts down without a error code sometimes. I have done allot of trial and error and have been up now for almost two straight days trying to figure this out.
Dale 2012 10-Jul-11 6:08am    
I have used the is ready property in my private sub draw item...... Is this incorrect to use it here?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900