How to add images in ComboBox dropdown items?
In WinForms ComboBox, 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
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.
Figure 1: ComboBoxAdv items with customized image
Samples:
C#
https://www.syncfusion.com/downloads/support/directtrac/139036/Combobox_ItemImage730382002.zip
VB
https://www.syncfusion.com/downloads/support/directtrac/139036/ComboBoxAdv_ItemImage_VB595294086.zip
Conclusion
I hope you enjoyed learning about how to add images in ComboBox dropdown items.
You can refer to our WinForms Combobox’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Combobox documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms Combobox and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!