Chapter 12 Working with Collections 301
- Click the Close button on the form.
The program ends.
Note The Text property changes made by the program have not been replicated on the
form within the Designer. Changes made at run time do not change the program’s core
property settings.
- Click the Save All button on the Standard toolbar to save your changes. Specify the
C:\Vb10sbs\Chap12 folder as the location.
Now you’re ready to try a different experiment with the Controls collection: using the Left
property to move each control in the Controls collection to the right.
Use a For Each... Next loop to move controls
- Display the form again, and then double-click the second button
object (Button2). - Type the following program code in the Button2_Click event procedure:
For Each ctrl In Controls
ctrl.Left = ctrl.Left + 25
Next
Each time the user clicks the second button, this For Each... Next loop steps
through the objects in the Controls collection one by one and moves them
25 pixels to the right. (To move objects 25 pixels to the left, you would subtract
25 instead .) A pixel is a device-independent measuring unit with which you can
precisely place objects on a form.
As in the previous event procedure that you typed, the ctrl variable is a
“stand-in” for the current object in the collection and contains the same
property settings as the object it represents. In this loop, you adjust the Left
property, which determines an object’s position relative to the left side of
the form.
- Click the Start Debugging button.
The program runs, and three buttons appear on the left side of the form.