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()
Try
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)
Else
ComboBox1.Items.Add(d.Name)
End If
Next d
ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
End If
Catch ex As IO.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
Return
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.DrawBackground()
e.Graphics.DrawIcon(icon, iconbox)
icon.Dispose()
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
e.DrawFocusRectangle()
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!