Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Display Images in ComboBox

Author: DevASP
Download Source Code : 365_Images_combobox.zip

In this simple article you will learn that how can you display images in a combobox this is a advance topic but with this article this will become very much simple for you.

Create a New Project in VB.net. Drag a ImageList control and combo box control from tool box and place on form. Now go to properties of the image list and using property collection ADD pictures. Now write code on form load event of the form and some other events as follows:

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

Dim items(Me.ImageList1.Images.Count - 1) As String

For i As Int32 = 0 To Me.ImageList1.Images.Count - 1

    items(i) = "Item " & i.ToString

Next

Me.ComboBox1.Items.AddRange(items)

Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

Me.ComboBox1.DrawMode = DrawMode.OwnerDrawVariable

Me.ComboBox1.ItemHeight = Me.ImageList1.ImageSize.Height

Me.ComboBox1.Width = Me.ImageList1.ImageSize.Width + 18

Me.ComboBox1.MaxDropDownItems = Me.ImageList1.Images.Count

 

End Sub

 

 

Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem

 

 If e.Index <> -1 Then

    e.Graphics.DrawImage(Me.ImageList1.Images(e.Index) _

     ,e.Bounds.Left, e.Bounds.Top)

 End If

 

End Sub

 

 

Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem

 

 e.ItemHeight = Me.ImageList1.ImageSize.Height

 e.ItemWidth = Me.ImageList1.ImageSize.Width

 

End Sub

Article Comments
Dear Friend/Sir,

Every topics are excellent. This is really very helpful for people like me.

Thank you Very Much
Bobby

Posted on 12/5/2006 3:34:56 PM by Bobby

Really superb! Nice work

Posted on 2/6/2007 6:59:21 AM by Mukesh

I want to display color image with corresponding color name im combo box.How can i add corresponding color name in combo box.If u know pls help me

Posted on 12/17/2007 3:32:14 AM by Deepthy

while handling this image combo, when a user clicks the image, i am getting an error in draw item as,"ArgumentOutofRangeException was unhandled". I have added 4 images in my list. and its says value '4' is not valid for 'index'.Please help me in this regards.
bhoopa

Posted on 12/18/2007 1:19:14 AM by Bhoopa

Very Helpful Article . Thanks a lot

Posted on 2/11/2008 5:59:40 AM by Rejeesh

simply superb

Posted on 3/31/2008 5:42:15 AM by sumi

hi
how scripts web page include ..

Posted on 11/8/2008 8:05:59 AM by codefox

Extremely good. Solved my problem of adding emoticons. Cheers!!
Thank you very much...

Posted on 2/3/2009 12:10:41 PM by Vrushank

Thank you much much

Posted on 1/27/2010 4:10:24 AM by DreamClown

Thanks the code is less and works, but how to add the text right next to image ?
M still searching.....

Posted on 2/2/2010 3:05:45 PM by Rizwan

i want image with text in combo box

Posted on 2/17/2010 11:46:51 PM by jimi

thank you very much

Posted on 3/17/2010 5:30:42 AM by Bmehar

In order to get image with text try this:

Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem

If e.Index <> -1 Then
e.Graphics.DrawImage(Me.ImageList1.Images(e.Index), _ e.Bounds.Left, e.Bounds.Top)
e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString(), ComboBox1.Font, _
System.Drawing.Brushes.Black, _
New RectangleF(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()
End If

Posted on 4/12/2010 8:52:21 AM by simbor

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to make XP Style Buttons in your VB.Net Application.

Disclaimer - Privacy
© 2002-2017 DevASP.net