Click here to Skip to main content
15,887,979 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok as one last attempt will someone please add or tell me what to change so that my drive combobox will display the format:

Icon LocalDisk C:\
Icon SecondDrive D:\
Icon CDROM E:\

I have submitted the code I have so far:


Imports System.IO
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Windows.Forms.Form
Imports System
Public Class Form1
    Public Function ListAllDrives() As String()
        Dim arDrives() As String
        arDrives = Directory.GetLogicalDrives()
        Return arDrives
    End Function


    Public Sub New()
        Call InitializeComponent()
        If LicenseManager.UsageMode <> LicenseUsageMode.Designtime Then
            For Each drive_info As DriveInfo In DriveInfo.GetDrives()
                ComboBox1.Items.Add(drive_info.Name & "   ")
                ComboBox1.Items.Add(drive_info.RootDirectory.ToString)
                If drive_info.IsReady() Then
                    ComboBox1.Items.Add(drive_info.VolumeLabel())
                End If
            Next drive_info
            ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        End If
    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



[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 8-Jul-11 22:11pm
v2
Comments
DaveAuld 9-Jul-11 4:32am    
What does the middle part represent (i.e. the LocalDisk, SecondDrive, CDRom) how do you want to determine that? Volume Names, Media Type?
Dale 2012 9-Jul-11 5:01am    
the middle part is only an example of the format im looking for.... It may or may not be named exactly as follows depending on what the user has named the C:\ drive or D:\ drive ect ect. I am looking to display the volume label but cannot seem to get it working

the closest example that I can find is this:

Public Class MainClass

Shared Sub Main()
For Each drive_info As DriveInfo In DriveInfo.GetDrives()
Console.Write(drive_info.Name & " " )
Console.WriteLine(drive_info.RootDirectory.ToString)
If drive_info.IsReady() Then
Console.WriteLine( drive_info.VolumeLabel())
End If
Next drive_info

End Sub

End Class


But it errors when I try to replace the console.write with combobox1.items.add because of the shared sub. If I change shared sub to public sub i get no error but the combobox on next debug comes up empty and then errors. Can you plz help?
Dale 2012 9-Jul-11 5:03am    
I have fixed the code I have so that it works to show the Icon corresponding to the correct drive and cdrom but still am unable to add the volume label here it is again:

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
Dim Drives() As DriveInfo = DriveInfo.GetDrives()
For Each Drive As DriveInfo In Drives
ComboBox1.Items.Add(Drive)
Next Drive
ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
End If
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
DaveAuld 9-Jul-11 5:16am    
Don't do that!!!! If you change something, improve the original question to update it with new information or tweaks to the code and text to say what you've do now.
Dale 2012 9-Jul-11 5:04am    
Thank you for everyone's help I hope to resolve this soon as it is probably trivial but very important to my efforts :)

Dale,

Looks like this has been done before;

Fast Drive ComboBox[^]

Maybe worth reading that and tweeking to fit your needs (if at all!). :)

There are also a few other articles referenced within this one.
 
Share this answer
 
Comments
Dale 2012 9-Jul-11 5:10am    
The solution is almost perfect and is exactly what I am looking to do but it is in C# and when I try to load the program into VB it has errors and does not show me where I am going wrong in my own code.
DaveAuld 9-Jul-11 5:18am    
C# object names are basically identical to VB, just look at what they are doing, follow the program flow, and then write your code in VB to the same structure adjusting the syntax to vb.net........
Maciej Los 9-Jul-11 13:04pm    
I agree with you DaveAuld, but in Fast Drive Combobox example author use own component. I think Dale is looking for solution without any other components.
DaveAuld 9-Jul-11 13:08pm    
No other component, its all source coded, take what he needs!
Dale 2012 9-Jul-11 16:50pm    
Losmac your correct I do not need any other components and it would be nice If I could load the project to see what the code is doing because when I do try it errors after converting it. Can I supply my project by email?...... can someone fix this and email it back to me?.... I have spent way to long and all examples so far have left me feeling more and more confused without any results.

thank you in advance
Take a look at solution 2 in your previous question: How To Display Drive Type In Combobox?[^], a specially at ImageCombobox in VB.NET[^]
 
Share this answer
 

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