Chapter 12 Working with Collections 299
process the individual objects of the collection. For example, you might want to change the
Enabled, Left, Top, Text, or Visible property of the objects in the collection, or you might want
to list the name of each object in a list box.
Experimenting with Objects in the Controls Collection
In the following exercises, you’ll use program code to manipulate the objects on a form by
using the Controls collection. The project you’ll create will have three button objects, and
you’ll create event procedures that change the Text properties of each object, move objects to
the right, and give one object in the group special treatment. The program will use three For
Each... Next loops to manipulate the objects each time the user clicks one of the buttons.
Use a For Each... Next loop to change Text properties
- Create a new Visual Basic Windows Forms Application project named My Controls
Collection. - Use the Button control to draw three button objects on the left side of the form, as
shown here: - Use the Properties window to set the Name property of the third button object
(Button3) to “btnMoveObjects .” - Double-click the first button object (Button1) on the form.
The Button1_Click event procedure appears in the Code Editor.
- Type the following program statements:
For Each ctrl In Controls
ctrl.Text = "Click Me!"
Next