384 Part III Designing the User Interface
- Double-click the Move Up button to edit its event procedure.
The Button1_Click event procedure appears in the Code Editor.
- Type the following program code:
GoingUp = True
Timer1.Enabled = True
This simple event procedure sets the GoingUp variable to True and enables the timer
object. The actual program code to move the picture box object and sense the correct
direction is stored in the Timer1_Tick event procedure. The GoingUp variable has
a jagged underline now because you have not declared it yet.
- Near the top of the form’s program code (below the statement Public Class Form1),
type the following variable declaration:
Dim GoingUp As Boolean 'GoingUp stores current direction
This variable declaration makes GoingUp available to all the event procedures in the
form, so the jagged underline in the Button1_Click event procedure is removed. I’ve
used a Boolean variable because there are only two possible directions for movement
in this program—up and down.
- Display the form again, double-click the Move Down button, and then enter the
following program code in the Button2_Click event procedure:
GoingUp = False
Timer1.Enabled = True
This routine is very similar to the Button1_Click event procedure, except that it changes
the direction from up to down.
- Display the form again, double-click the Timer1 object, and then enter the following
program code in the Timer1_Tick event procedure:
If GoingUp = True Then
'move picture box toward the top
If PictureBox1.Top > 10 Then
PictureBox1.Location = New Point _
(PictureBox1.Location.X - 10, _
PictureBox1.Location.Y - 10)
End If
Else
'move picture box toward the bottom
If PictureBox1.Top < (Me.Size.Height - 75) Then
PictureBox1.Location = New Point _
(PictureBox1.Location.X + 10, _
PictureBox1.Location.Y + 10)
End If
End If