Friday, April 24, 2009

Display Image in ComboBox

In case we need to display a list of images in ComboBox we can do it by caring just few things about combobox. Firtsly set the 'DrawMode' of the ComboBox to 'OwnerDrawFixed' if all the images are of same size or set 'OwnerDrawVariable' if images are of variable size. and also for the variable sized images set the width and height during the 'MeasureItem' event of the combobox. If imgList1 contains all the needed images of variable and let number of items in the combobox is equal to number of images in the imagelist and each index of image specifies the corresponding item in combo box, Then I guess the following will be fine to get the list of images in the combobox.

1. Set the property 'DrawMode' to OwnerDrawVariable
2. In the method that implement event 'DrawItem' of the combo box, for the case if index of current item is non empty then then draw the image. i.e.

If e.Index<>-1 Then
e.Graphics.DrawImage(imgList1.images(e.Index),e.Bounds.Left,e.Bounds.Top)
End If

3. In the method that implements the 'MeasureItem' event of the combo box the following can be used
e.ItemHeight =imgList1.ImgaeSize.Height
e.ItemWidth = imgList1.ImageSize.Width


These are the basic settings that are needed to display images in combo box, rest are depends on the requirements.

Drop the lines if you think that can be help better for similar kind of requirements.

No comments: