Results 1 to 3 of 3

Thread: ComboBox With Images

  1. #1

    Thread Starter
    Member raineym's Avatar
    Join Date
    Dec 2006
    Location
    Roanoke Rapids, NC
    Posts
    54

    ComboBox With Images

    Written in VS2008

    This is my first control, so be gentle. It was created for a customer I'm writing an IT Work Order program for who wanted to show an image beside the Priority and Sub-Priority items.

    The idea is based on NickThissen's ColorListBox control.

    Here is a very simple owner-drawn ComboBox control that allows you to specify some Text (as normal) and an Image for each item. You can also align the Image to the Left or Right side of each item with the ImageAlign property.

    How it works:

    Akin to NickThissen's ColorListBox, the ImageComboBox class inherits ComboBox, and overloads the Items property. Instead of returning MyBase.Items, it returns its own ImageComboBoxItemCollection.

    The original base items are then returned by a (private) property baseItems.

    VBnet Code:
    1. ''' <summary>
    2.   ''' Returns the items in the ImageComboBoxItemCollection to be used in the ImageComboBox.
    3.   ''' </summary>
    4.   <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    5.   Public Overloads ReadOnly Property Items() As ImageComboBoxItemCollection
    6.     Get
    7.       Return _Items
    8.     End Get
    9.   End Property
    10.  
    11.   ''' <summary>
    12.   ''' The original items from the ImageComboBox that will never been seen.
    13.   ''' </summary>
    14.   Private ReadOnly Property baseItems() As ObjectCollection
    15.     Get
    16.       Return MyBase.Items
    17.     End Get
    18.   End Property

    The ImageComboBoxItemCollection inherits System.Collections.ObjectModel.Collection(Of ImageComboBoxItem) and overrides the Set/Remove/InsertItem and ClearItems methods. In those methods, it adds the item to it's collection but also adds the item to the baseItems property of the owner ImageComboBox. This is necessary because the ImageComboBox won't draw any items in your own custom collection; only those in the MyBase.Items collection.

    VBnet Code:
    1. ''' <summary>
    2.     ''' Clears all the ImageComboBoxItems from both the ImageComboBoxCollection and the baseItems list of the ImageComboBox.
    3.     ''' </summary>
    4.     ''' <remarks></remarks>
    5.     Protected Overrides Sub ClearItems()
    6.       MyBase.ClearItems()
    7.       _comboBox.baseItems.Clear()
    8.     End Sub
    9.  
    10.     ''' <summary>
    11.     ''' Inserts an ImageComboBoxItem at the specified index into the ImageComboBoxCollection and the baseItems list of the ImageComboBox.
    12.     ''' </summary>
    13.     ''' <param name="index">Index at which to insert the ImageComboBoxItem.</param>
    14.     ''' <param name="item">The ImageComboBoxItem to be inserted.</param>
    15.     ''' <remarks></remarks>
    16.     Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As ImageComboBoxItem)
    17.       MyBase.InsertItem(index, item)
    18.       _comboBox.baseItems.Insert(index, item)
    19.     End Sub
    20.  
    21.     ''' <summary>
    22.     ''' Removes an ImageComboBoxItem at the specified index from the ImageComboBoxCollection and the baseItems list of the ImageComboBox.
    23.     ''' </summary>
    24.     ''' <param name="index">Index of the ImageComboBoxItem to be removed.</param>
    25.     ''' <remarks></remarks>
    26.     Protected Overrides Sub RemoveItem(ByVal index As Integer)
    27.       MyBase.RemoveItem(index)
    28.       _comboBox.baseItems.RemoveAt(index)
    29.     End Sub
    30.  
    31.     ''' <summary>
    32.     ''' Replaces an ImageComboBoxItem at the specified index with another ImageComboBoxItem in the ImageComboBoxCollection and the baseItems list of the ImageComboBox.
    33.     ''' </summary>
    34.     ''' <param name="index"></param>
    35.     ''' <param name="item"></param>
    36.     ''' <remarks></remarks>
    37.     Protected Overrides Sub SetItem(ByVal index As Integer, ByVal item As ImageComboBoxItem)
    38.       MyBase.SetItem(index, item)
    39.       _comboBox.baseItems(index) = item
    40.     End Sub

    Finally, the ImageComboBox is owner drawn, so it overrides the OnDrawItem method. The combobox actually wants to draw the original items, but I simply force it to use the items from my own collection (by using their index), and draw them with the correct text and with an image.

    Even with all this, you can pick and choose which items display an Image by leaving the ComboListBoxItem's Image property blank.

    Enjoy!
    Attached Files Attached Files
    Michael Rainey
    --------------------
    OS: Windows 11 Professional
    VS: 2022
    Level: Intermediate

    Controls: ImageComboBox

  2. #2
    New Member
    Join Date
    Sep 2011
    Posts
    1

    Re: ComboBox With Images

    Great code and article!

    one minor suggestion:

    in Class ImageComboBoxItem

    add

    Public Overrides Function ToString() As String
    Return _text
    End Function ' ToString

    makes it much easier on the eyes in designer.

  3. #3
    New Member
    Join Date
    Dec 2009
    Posts
    13

    Re: ComboBox With Images

    nice job,

    i'll have much use for this,

    question, i noticed "ComboBoxStyle.DropDownList" is hidden, so that it always shows "DropDownList", however it doesn't appear to be themed properly like the normal combobox does.

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width