Hi, Hello there!
Through this tutorial, today I'll teach you how to create a bouncing ball program in microsoft visual basic 2008/ 2010.
Start a new project at Visual Studio 2010 and select Visual Basic from the left panel and select Windows Forms Application from the left panel, then click OK.
A new project workspace with a new form will appear.
Now add a picture box in the form from the toolbox.
Then add a picture of earth in the picture box. Click here to download the picture of earth.
Do something like this:
Now open the "toolbox" and double click on the "Timer" control and do this for four times to add four timers to the form. Select each Timer and then change their "Interval" propery to 1.
Now select only the first timer that means Timer1 and change its "Enabled" property to "True".
THE CODING PART:
Let's add the code to each timer tick event. To do this, double click on each label and add the red color highlighted code given below accordingly.
Double click Timer1 and add the highlighted code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X + 5, PictureBox1.Location.Y + 5)
If PictureBox1.Location.X + PictureBox1.Width > Me.Width Then
Timer1.Stop()
Timer2.Start()
End If
If PictureBox1.Location.Y + PictureBox1.Height > Me.Height Then
Timer1.Stop()
Timer3.Start()
End If
End Sub
Double click Timer2 and add the highlighted code:
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X - 5, PictureBox1.Location.Y + 5)
If PictureBox1.Location.Y + PictureBox1.Height > Me.Height Then
Timer2.Stop()
Timer4.Start()
End If
If PictureBox1.Location.X < 0 Then
Timer2.Stop()
Timer1.Start()
End If
End Sub
Double click Timer3 and add the highlighted code:
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X + 5, PictureBox1.Location.Y - 5)
If PictureBox1.Location.X + PictureBox1.Width > Me.Width Then
Timer3.Stop()
Timer4.Start()
End If
If (PictureBox1.Location.Y < 0) Then
Timer3.Stop()
Timer1.Start()
End If
End Sub
Double click Timer4 and add the highlighted code:
Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X - 5, PictureBox1.Location.Y - 5)
If PictureBox1.Location.X < 0 Then
Timer4.Stop()
Timer3.Start()
End If
If (PictureBox1.Location.Y < 0) Then
Timer4.Stop()
Timer2.Start()
End If
End Sub