VBA TIPS
Using the ImageCombo Control (a ComboBox Control with images)

As an AxtiveX control, do you know that the handy control which can display a picture is prepared? Although that control is very similar to a standard ComboBox Control, but it is completely a different control named ImageCombo Control.

This control can include pictures with each item in the list portion of the combo like the image below. By using this ImageCombo control, the users can identify and choose items from a list of possible selections easily. Because each item has an image!



Firstly you have to add 2 controls from the ToolBox in the Visual Basic Editor.

Right click on the ToolBox dialogue then select Additional Controls...

Next, tick the following controls from the Available Controls list.
  • Microsoft ImageComboBox Control 6.0(SP4) - the version would be depending on your environment.
  • Microsoft ImageList Control 6.0(SP4) - the version would be depending on your environment.


Now tow additional controls have been added as the image on the right.





By the way, why do you need an ImageList Control? An ImageList control contains a collection of images that can be used by the ImageComboBox Control. (and by the other Windows Common Controls and by controls with a Picture property, such as ListView control, TreeView control, Toolbar control and TabStrip control.)



Example

Okay, let's practice! In this example, all images are placed in Sheet1. (of cource you can use images from your Hard Disk using LoadPicture method, but I like this way because I feel it's handy)

Add eight image controls on the worksheet. Name them Image1 to Image8.

You can add image controls View - Toolbars - Control Toolbox from main menu bar.

Right click for Propertiesof the image control and select appropriate image file (with an extension of bmp, cur, ico, jpg or gif) from picture property box in the properties window.

Then, place the object name in the B2 to B9 cells.

In the cells C2 to C9, place text for displaing to the ImageCombo Control.

Now you can see the picture on the Sheet1 as follows. Note, this is a preparation for using a loop later.




Place the following in an Userform module.



Remarks

In the line Dim imgLst As New ImageList, making an instance of ImageList Control.

Next, using the Add method, enter the Images to the ListImages Collection Object of the ImageList Control. the key must be unique.

Relates ImageCombo Control to ImageList Control in the Set ImageCombo1.ImageList = myImageList line.

At last using the Add method, relates each ComboItems to images. Setting an image to an argument named SelImage:=, the image will be change when each item is selected.

When you'd like to get the selected value, since the ImageCombo control has not the Value Property, use Text property instead.



Download

You can download the sample workbook from here. sample_069.zip

Option Explicit


Private Sub UserForm_Initialize()

    Dim imgLst As New ImageList
    Dim aObjName As Variant
    Dim aText As Variant
    Dim i As Long

    aObjName = Sheet1.Range("B2:B9").Value
    aText = Sheet1.Range("C2:C9").Value

    For i = 1 To 8
        imgLst.ListImages.Add _
        Key:="img" & i, _
        Picture:=Sheet1.OLEObjects(aObjName(i, 1)).Object.Picture
        'You can use LoadPicture for setting the Picture property
    Next

    Set ImageCombo1.ImageList = imgLst

    For i = 1 To 7
        ImageCombo1.ComboItems.Add _
        Index:=i, _
        Text:=aText(i, 1), _
        Image:="img" & i, _
        SelImage:="img8"
    Next

    Set imgLst = Nothing
End Sub


Private Sub CommandButton1_Click()
    If Not ImageCombo1.SelectedItem Is Nothing Then
        MsgBox ImageCombo1.SelectedItem.Text
    End If
End Sub

| HOME |
Copyright © cellmasters.net - colo's junk room All Right Reserved
ABOUT
WORKS
THE CELL MASTERS
CONTACT
LINKS
Tips and Information about Microsoft Excel|Masaru Kaji aka Colo