)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add images in ComboBox dropdown items?

Platform: WinForms |
Control: ComboBoxAdv |
Published Date: May 27, 2015 |
Last Revised Date: May 22, 2019

In ComboBoxAdv, you can load image in its TextBox and customize each of its items appearance by handling its DrawItem event and ShowImageInTextBox property. The following code example is to demonstrate the same.

C#

//To Display the image in TextBox
this.comboBoxAdv1.ShowImageInTextBox = true;
this.comboBoxAdv1.ListBox.DrawItem += new DrawItemEventHandler(ListBox_DrawItem);
//Handling the DrawItem event
void ListBox_DrawItem(object sender, DrawItemEventArgs e)
{
    ListBox list = sender as ListBox;
    string text = list.Items[e.Index].ToString();
    if (e.Index < 0)
        return;
    list.ItemHeight = 12;
    Rectangle Bounds = new Rectangle(40, 1, 10, 10);
    if (list.Items[0].ToString() == text)
    {
        //To Draw the image in listBox
        e.Graphics.DrawImage(this.imageList1.Images[0], Bounds);               
    }
    Bounds.Y += 12;
    if (list.Items[1].ToString() == text)
    {
        e.Graphics.DrawImage(this.imageList1.Images[1], Bounds);
    }
    Bounds.Y += 12;
    if (list.Items[2].ToString() == text)
    {
        e.Graphics.DrawImage(this.imageList1.Images[2], Bounds);            
    }
    Bounds.Y += 12;
    if (list.Items[3].ToString() == text)
    {
        e.Graphics.DrawImage(this.imageList1.Images[3], Bounds);
    }
    Bounds.Y += 12;
    if (list.Items[4].ToString() == text)
    {
        e.Graphics.DrawImage(this.imageList1.Images[0], Bounds);
    }
}

VB

'To Display the image in TextBox
Me.comboBoxAdv1.ShowImageInTextBox = True
AddHandler comboBoxAdv1.ListBox.DrawItem, AddressOf ListBox_DrawItem
'Handling DrawItem event
Private Sub ListBox_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
 Dim list As ListBox = TryCast(sender, ListBox)
 Dim text As String = list.Items(e.Index).ToString()
 If e.Index < 0 Then
  Return
 End If
 list.ItemHeight = 12
 Dim Bounds As New Rectangle(40, 1, 10, 10)
 If list.Items(0).ToString() = text Then
  'To Draw the image in listBox
  e.Graphics.DrawImage(Me.imageList1.Images(0), Bounds)
 End If
 Bounds.Y += 12
 If list.Items(1).ToString() = text Then
  e.Graphics.DrawImage(Me.imageList1.Images(1), Bounds)
 End If
 Bounds.Y += 12
 If list.Items(2).ToString() = text Then
  e.Graphics.DrawImage(Me.imageList1.Images(2), Bounds)
 End If
 Bounds.Y += 12
 If list.Items(3).ToString() = text Then
  e.Graphics.DrawImage(Me.imageList1.Images(3), Bounds)
 End If
 Bounds.Y += 12
 If list.Items(4).ToString() = text Then
  e.Graphics.DrawImage(Me.imageList1.Images(0), Bounds)
 End If
End Sub

 

Note:

You can customize each item of the ComboBoxAdv according to your requirement, by handling the DrawItem event handler. For example, each item of the ComboBoxAdv can be drawn with different backcolor and forecolor.

 

Showing ComboBoxAdv items with customized image

Figure 1: ComboBoxAdv items with customized image

Samples:

C#

http://www.syncfusion.com/downloads/support/directtrac/139036/Combobox_ItemImage730382002.zip

VB

http://www.syncfusion.com/downloads/support/directtrac/139036/ComboBoxAdv_ItemImage_VB595294086.zip

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon