Click here to Skip to main content
11,629,614 members (79,755 online)
Click here to Skip to main content

Tagged as

Adding images to a combobox control

, 19 Feb 2013 CPOL 4.5K 4
Rate this:
Please Sign up or sign in to vote.
Adding images to a combobox control.

This may sound a lot harder than it actually is.  So let's take a look at how to do it:

First, create or add a combobox to your form. You need to change DrawMode to DrawMode.OwnerDrawFixed or DrawMode.OwnerDrawVariable. The DrawMode.OwnerDrawFixed enumeration draws all of the combobox items at the same size. The DrawMode.OwnerDrawVariable enumeration draws each of the combobox items at its own size (although they can all be the same size). I also set the ItemHeight to 100 to allow the image enough room to be displayed.  You can also change the MaxDropDownItems to limit the number of items to something less or more than the standard 8 items.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  Dim oXref As Xrefs
  Dim oBitmap As System.Drawing.Bitmap

  With ComboBox1
    .DrawMode = DrawMode.OwnerDrawVariable
    .ItemHeight = 100
    .DisplayMember = "Text"
    .ValueMember = "Image"
    oBitmap = My.Resources.faceplate001
    oXref = New Xrefs("Item 1", oBitmap)
    .Items.Add(oXref)
    oBitmap = My.Resources.faceplate002
    oXref = New Xrefs("Item 2", oBitmap)
    .Items.Add(oXref)
    oBitmap = My.Resources.faceplate003
    oXref = New Xrefs("Item 3", oBitmap)
    .Items.Add(oXref)
  End With
End Sub

Just to make it easier for me when I get data back from the combobox, I am using a class to contain related data:

Friend Class Xrefs
  Public Property Text As String
  Public Property Image As Image
  Public Sub New()
    Me.New(Nothing, Nothing)
  End Sub
  Public Sub New(ByVal Text As String)
    Me.New(Text, Nothing)
  End Sub
  Public Sub New(ByVal Text As String, ByVal Image As Image)
    Me.Text = Text
    Me.Image = Image
  End Sub
End Class

The only thing left to do is draw the image as required by the control:

Private Sub ComboBox1_DrawItem(sender As System.Object, _
       e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
  If e.Index > -1 Then
    Dim oXref As Xrefs = ComboBox1.Items(e.Index)
    e.DrawBackground()
    e.Graphics.DrawImage(oXref.Image, e.Bounds.Left, e.Bounds.Top)
  End If
End Sub

You need to call db so that the control paints the background properly.  This will include the times the mouse moves over or through an item in the drop down list.

This is what my example looks like:

License

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

Share

About the Author

Adam Zuckerman
Software Developer (Senior)
United States United States
Long time software engineer who rambles occasionally about coding, best practices, and other random things.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150723.1 | Last Updated 19 Feb 2013
Article Copyright 2013 by Adam Zuckerman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid