Click here to Skip to main content
13,256,260 members (47,493 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 19 Feb 2013

Adding images to a combobox control

, 19 Feb 2013
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)
    oBitmap = My.Resources.faceplate002
    oXref = New Xrefs("Item 2", oBitmap)
    oBitmap = My.Resources.faceplate003
    oXref = New Xrefs("Item 3", oBitmap)
  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.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:


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


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 --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 19 Feb 2013
Article Copyright 2013 by Adam Zuckerman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid