Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have been searching for days now to properly add Images and text to a combobox. Currently the code I am trying to use only displays the image without the logical drives that I have told it to list at program load. Why is this happening?

here is the code Im using as of right now

VB.NET
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add(System.IO.Directory.GetLogicalDrives)
        Dim items(Me.ImageList1.Images.Count - 1) As String
        For i As Int32 = 0 To Me.ImageList1.Images.Count - 1
            items(i) = "Item " & i.ToString
        Next
        Me.ComboBox1.Items.AddRange(items)
        Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        Me.ComboBox1.DrawMode = DrawMode.OwnerDrawVariable
        Me.ComboBox1.ItemHeight = Me.ImageList1.ImageSize.Height
    End Sub
    Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
        If e.Index <> -1 Then
e.Graphics.DrawImage(Me.ImageList1.Images(e.Index) _
,e.Bounds.Left, e.Bounds.Top)
        End If
    End Sub
    Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem
        e.ItemHeight = Me.ImageList1.ImageSize.Height
        e.ItemWidth = Me.ImageList1.ImageSize.Width
    End Sub
End Class


Please help me in knowing what Im missing.

Thank you very much for your help.
Posted
Updated 6-Jun-11 2:54am
v2

Hello,

Showing icon along data is not possible with the current combo box control. You need to write your own combo box control capable of showing icons in it.
Below is an example:
Image ComboBox Control[^]

Hope this answers your question
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jun-11 18:19pm    
Great link my 5, but... why pushing the limits of Forms? What was good at the time of v.1.1 looks ridiculous now. WPF is much better.

Please see my solution.
--SA
If you have gone so far with System.Windows.Forms, you gone too far. :-)
You should switch to WPF where the content model (http://msdn.microsoft.com/en-us/library/bb613548.aspx[^]) is so liberal that such things are more than natural.
Why pushing the limits of Forms and almost abusing it, when a switch to WPF will give you a very regular use of the feature?

How about that?

—SA
 
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