Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

302 Part II Programming Fundamentals



  1. Click the first button, and then click the second button several times.


The buttons on the form change to “Click Me!”, and then each time you click the
second button, the objects on the form gradually move to the right. Your screen looks
like this after five clicks:


  1. Click the Close button on the form to stop the program.

  2. Click the Save All button to save your changes.


You won’t always want to move all the objects on a form as a group. With Visual Basic,
you can process collection members individually. In the next exercise, you’ll learn
how to keep the third button object in one place while the other two buttons move
to the right.

Using the Name Property in a For Each Next Loop


If you want to process one or more members of a collection differently than you process the
others, you can use the Name property, which uniquely identifies each object on the form.
You’ve set the Name property periodically in this book to make your program code more
readable, but Name also can be used programmatically to identify specific objects in your
program.

To use the Name property programmatically, single out the objects to which you want to
give special treatment, and then note their Name properties. Then as you loop through the
objects on the form by using a For Each... Next loop, you can use one or more If statements
to test for the important Name properties and handle those objects differently. For example,
let’s say you want to construct a For Each... Next loop that moves one object more slowly
across the form than the other objects. You could use an If... Then statement to spot the
Name property of the slower object and then move that object a shorter distance, by not
incrementing its Left property as much as those of the other objects.
Free download pdf