Windows Controls: The Image List



In some application that deals with graphic, you may use only one picture but in various, if not most other applications, such as those used to display, exchange, or switch pictures, you may deal with different pictures. In such a type of application, sometimes the pictures are of different sizes, even including unpredictable sizes. In another type of application, some of the pictures you use may have the (exact) same size. For this type, instead of using each picture individually, you can store them in a collection, then access each picture when needed.

An image list is a collection of icons or pictures that are stored so that each icon or picture can be located by an index. The icons or pictures must be of the same size and they should be the same type. This means that the collection can be made of only icons of 16x16 sizes, only icons of 32x32 sizes, only icons of 48x48 sizes, or only bitmaps (but of the same size each).

When it comes to design, there are two types of image lists. Each picture can be added individually to the collection, or all of the pictures can be designed in a longer picture and, inside of that long picture, you would use a technique to locate each picture.

Once you get an image list, there is no pre-defined way you must use it. Some Windows controls use an image list to use the pictures they need but there are various other unpredictable ways you can use an image list.

Practical LearningPractical Learning: Introducing Image Lists

  1. To create a new application, on the main menu, click File -> New -> Project...
  2. In the Templates list, click Windows Application
  3. Set the name to ImageViewer1 and click OK

Creating an Image List

To support image lists, the .NET Framework provides a class called ImageList that is defined in the System.Windows.Forms namespace of the System.Windows.Forms.dll library. At design time, to create an image list, from the Components section of the Toolbox, you can click the ImageList button  and click the form.

To programmatically create an image list, declare a variable of type ImageList and initialize it using one of its two constructors. The default constructor is used to simply signal that you are going to use an image list. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Private lstImages As ImageList

        Dim components As System.ComponentModel.Container

        Public Sub New()
        End Sub

        Public Sub InitializeComponent()

            lstImages = New ImageList()

        End Sub

    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter


        Return 0
    End Function

End Module

The ImageList class is equipped with another constructor whose syntax is:

Public Sub New(container As IContainer)

This constructor expects as argument the control container that will be responsible for getting rid of the image list when the application exits.

Practical LearningPractical Learning: Creating an Image List

  1. From the Components section of the Toolbox, click ImageList and click the form
  2. In the Properties window, change its Name to lstPictures
  3. From the Components section of the Toolbox, click Timer and click the form
  4. In the Properties window, change its characteristics as follows:
    Enabled: True
    Interval: 2000
  5. From the Common Controls section of the Toolbox, click PictureBox and click the form
  6. In the Properties window, change its characteristics as follows:
    (Name): pbxViewer
    Dock: Fill
  7. Save all



Home Copyright © 2008-2016, FunctionX, Inc. Next