JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Using Image Lists with Picture Boxes and Other Controls with Image or BackgroundImage Properties

Although image lists were primarily designed to work with controls that have an ImageList and ImageIndex property (see the previous topic), you also can use image lists with controls that only have Image or BackgroundImage properties.

For example, picture boxes only have an Image property, so to load an image from an image list into a picture box, you have to access images in the list using the Images collection. Here's how that works in the ImageLists example on the CD-ROM, where I'm loading image 0 from ImageList1 into a picture box when the form loads, and cycling through the other images in the image list when the user clicks a "New image" button beneath the picture box:

Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    PictureBox1.Image = ImageList1.Images(0)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
    Static ImageIndex As Integer = 0
    If ImageIndex < ImageList1.Images.Count - 1 Then
        ImageIndex += 1
        ImageIndex = 0
    End If
    PictureBox1.Image = ImageList1.Images(ImageIndex)
End Sub

You can see the results of this code in Figure 10.1—the picture box is the one in the middle of the figure.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor