Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating Bouncing Ball Application in VB.Net

Author: DevASP
Download Source Code : 524_Bounce_ball.zip

In this sample application i will try to demonstrate that how you can implement timer in your application using Visual Basic.Net

Open Visual Studio.NET and create a new application using vb.net. Drag the Group box Control, Timer Control and two buttons and a picture box with in the group box control

Set the back color of the from to white and set back color of the picture box transparent

Now declare the following global variable

 

Dim ffturn As Boolean = False

 

Double click on from and write the following code on from load event

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        PictureBox1.Visible = True

        PictureBox1.Left = GroupBox1.Width

        PictureBox1.Top = GroupBox1.Height

End Sub

 

We restrict image with in the group box so that it can only move with in the Group box and deflects from its wall

 

Double click on the Start Button and write the following code button click event which basically helps to start the movement of the ball

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Timer1.Enabled = True

End Sub

Double click on the Stop Button and write the following code button click event which helps to stop the movement of the ball

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Timer1.Enabled = False

End Sub

 

Now double click on the timer and write the following code which controls the over all movement of ball, its bounce and reflection

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

 

        Static Dim cxClient, cyClient, xCenter, yCenter, cxTotal, cyTotal As Integer

        Static Dim cxRadius As Integer

        Static Dim cyRadius As Integer

        Static Dim cxMove As Integer

        Static Dim  cyMove As Integer

        Static Dim cx As Integer

        Static Dim cy As Integer

        Static Dim x1 As Integer

        Static Dim y1 As Integer

        Static Dim x2 As Integer

        Static Dim y2 As Integer

       

        Dim GW, GH

 

        If ffturn = False Then

            ffturn = True

            cxClient = GroupBox1.Width

            cyClient = GroupBox1.Height

 

            xCenter = cxClient / 2

            yCenter = cyClient / 2

 

            cxMove = 10

            cyMove = 10

 

            cxTotal = 2 * cxMove

            cyTotal = 2 * cyMove

 

            PictureBox1.Left = xCenter

            PictureBox1.Top = yCenter

        End If

 

        x1 = xCenter - cxTotal / 2

        y1 = yCenter - cyTotal / 2

 

 

        PictureBox1.Left = x1

        PictureBox1.Top = y1

 

 

        xCenter += cxMove

        yCenter += cyMove

 

        If ((xCenter + 15 >= cxClient) Or (xCenter <= GroupBox1.Top)) Then

            cxMove = -cxMove

        End If

 

        If ((yCenter + 15 >= cyClient) Or (yCenter <= GroupBox1.Top)) Then

            cyMove = -cyMove

        End If

      End Sub

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating Attractive Buttons in Visual Basic.Net(VB.Net)

Disclaimer - Privacy
© 2002-2017 DevASP.net