How To Programmatically Add Images to ImageList Control

This article was previously published under Q166937
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes how you can programmatically add images to theImageList ActiveX control.
An ImageList control contains a collection of ListImage objects, each ofwhich can be referred to by its index or key. The ImageList ActiveX controlacts as a central image repository to conveniently supply other controlswith images.

In order to add an image at run time, you need to use the Add Method of theListImages collection.


The Add method syntax has these parts:
Object.Add([Index,| Key,] Picture)
  • Index - Optional. An integer specifying the position where you want to insert the ListImage. If no index is specified, the ListImage is added to the end of the ListImages collection.
  • Key - Optional. A unique string that identifies the ListImage object. Use this value to retrieve a specific ListImage object. An error occurs if the key is not unique.
  • Picture - Required. Specifies the picture to be added to the ImageList ActiveX Control.


The steps below demonstrate how to create an example of one of thesecontrols:

  1. Create a form.
  2. Add an ImageList ActiveX control to the form and change the ImageList ActiveX control name to ImageList1.
  3. Add a SSTab ActiveX control and change the SSTab control name to SSTab1. By default, SSTab always has three pages. If using Visual FoxPro 5.0a, the SSTab control has been renamed to Microsoft Tabbed Dialog Control, version 5.0. Other development tools, such as Visual Basic, may have replaced the SSTab control with the Microsoft Tabbed Dialog Control even if the Visual FoxPro version is 5.0 (not 5.0a).
  4. Add a Command button and change the Caption of the command button to "Click Me."
  5. In the INIT method of the form, put in the following code:
          Thisform.ImageList1.Object.ListImages.Add(1,,LoadPicture;            (HOME()+"samples\graphics\bmps\outline\leaf.bmp"))      Thisform.ImageList1.Object.ListImages.Add(2,,LoadPicture;            (HOME()+"samples\graphics\bmps\outline\open.bmp"))      Thisform.ImageList1.Object.ListImages.Add(3,,LoadPicture;            (HOME()+"samples\graphics\bmps\outline\closed.bmp"))								
  6. In the CLICK method of the Command button, put in the following code:
           FOR i = 0 TO 2           Thisform.SSTab1.Object.Tab = i           Thisform.SSTab1.Object.Picture = ;             Thisform.ImageList1.ListImages(i+1).Picture       ENDFOR       Thisform.SSTab1.Object.Tab = 0								
  7. Save and run the form. Click the "Click Me" button, and the images will display in the SSTab ActiveX control.
NOTE: Due to the Add method requiring an object reference for the imagefile, you need to use the LoadPicture function to return the objectreference instead of directly using the image file.

You are not limited to any particular image size; however, the total numberof images that can be loaded into ImageList ActiveX control is limited bythe amount of available memory.

At design time, you can add images using the Image tab of the ImageListControl Properties dialog box. To access this, right-click (secondary) onthe ImageList control and select ImageListCtrl Properties from the shortcutmenu. Omit step 5 above to have images added in this manner show on thetabs when the Command button is clicked.

The code in step 6 above could be placed in the Init method of the form tohave the SSTab control show the images when the form is loaded.
Custom Control Reference (Ctrlref.hlp in the Windows\System orWinNT\System32 folder if using Visual FoxPro version 5.0)

Windows Common Controls help file (Comctl1.hlp in the Windows\System orWinNT\System32 folder if using version 5.0a).

To access the Help file, right-click on the ImageList control on the formand select Help from the shortcut menu. In the ImageList Control topic,click See Also to jump to the ListImage Object, ListImages Collectiontopic. Select the Add Method (ListImages Collection) from the Methods jump.

Article ID: 166937 - Last Review: 02/24/2014 08:28:05 - Revision: 1.2

  • Microsoft Visual FoxPro 5.0 Standard Edition
  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 Professional Edition
  • kbnosurvey kbarchive kbhowto kbinterop KB166937