View contents of this chapter

Control Arrays

A control array isn't conceptually much different from any other type of array, such as an array of integers, which you learned in the previous chapter. Like an array of integers, a control array is a grouping of identical variables, except that in the case of a control array, the variables are control object variables that contain references to control objects, rather than just integers. Like an array of integers, each individual control object is accessed by its index number.

Control arrays are useful when you don't know in advance how many instances of a control you will need.

Slide Show Example

Create a new project and add an Image control to the form. Change the name of the Image control to "imgGraphics", and set its visible property to false. Now copy that image control and paste the image control to the form. VB displays the message box shown in the following figure:

If you answer Yes, VB automatically creates a control array with a name that matches the first control. For example, if the first control is named Image1, the array is named Image1, and the elements begin at Image1(0) and increment as long as you keep pasting the control. Paste one more image control to the form.

Now add a timer control and another Image control using the tollbox. Set the following properties:

Control Property Value
Form

Name

frmShow

Form

Caption

Slide Show

Timer Enabled True
Timer Interval 1000
Image Control Name imgViewer

The properties for the Image control array

  Property Value
imgGraphics(0)

Picture

1.jpg

imgGraphics(1)

Picture

2.jpg

imgGraphics(2)

Picture

3.jpg

The source code needed for this example is stored in the
"Source Code\Chapter14\Slide Show" directory. To open the project double click the "SlideShow.vbp" file.



© Universal Teacher Publications        INDEX Previous Screen Next Screen