Chapter 15 Adding Graphics and Animation Effects 381
To move a relative distance to the right or left, you would add or subtract pixels from the
current Left property setting. For example, to move an object 50 pixels to the right, you add
50 to the Left property, as follows:
PictureBox1.Left = PictureBox1.Left + 50
In a similar way, you can change the vertical location of an object on a form by setting the
Top property, which takes the syntax:
object.Top = vertical
where object is the name of the object on the form that you want to move, and vertical is
the new vertical, or y-axis, coordinate of the top edge of the object, measured in pixels.
For example, the following program statement moves a picture box object to a location
150 pixels below the window’s title bar:
PictureBox1.Top = 150
Relative movements down or up are easily made by adding or subtracting pixels from the
current Top property setting. For example, to move 30 pixels in a downward direction, you
add 30 to the current Top property, as follows:
PictureBox1.Top = PictureBox1.Top + 30
The Location Property
To move an object in both vertical and horizontal directions, you can use a combination of
the Left and Top property settings. For example, to relocate the upper-left corner of a picture
box object to the (x, y) coordinates (300, 200), you enter the following program code:
PictureBox1.Left = 300
PictureBox1.Top = 200
However, the designers of Visual Studio don’t recommend using two program statements
to relocate an object if you plan to make numerous object movements in a program (for
example, if you plan to move an object hundreds or thousands of times during an elaborate
animation effect). Instead, you should use the Location property with the syntax:
object.Location = New Point(horizontal, vertical)
where object is the name of the object, horizontal is the horizontal x-axis coordinate, vertical
is the vertical y-axis coordinate, and Point is a structure identifying the pixel location for
the upper-left corner of the object. For example, the following program statement moves
a picture box object to an (x, y) coordinate of (300, 200):
PictureBox1.Location = New Point(300, 200)