VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code
<

Enhanced Animation Program

Visual Basic animation with interactive demo and VB.NET implementation


This enhanced animation program demonstrates how to create movement in Visual Basic using timers and the Move method. In this program, you need to insert four timers and make them invisible with their Enabled property set to false at startup. The program uses the Move method and the computer coordinate system (Left, Top) to change the positions of the image.

The basic syntax of the Move method is object.Move Left, Top. For example, Image1.Move 1000,1000 will move the object Image1 to the location (1000,1000). I've created four sub procedures Move_up, Move_down, Move_left and Move_right to move the image in the respective directions.

Key Concepts

  • Using timers to create animation effects
  • Understanding the coordinate system in Visual Basic
  • Implementing boundary checking
  • Resetting object position when boundaries are reached
  • Controlling multiple timers with buttons

Interactive Animation Demo

Try the animation below! Click the direction buttons to move the box. The box will reset to the center when it reaches the boundary.

This demo simulates the VB6 animation using JavaScript. The original VB6 program uses timers to create continuous movement.

Visual Basic Implementation

VB6 Animation Code
Dim x As Integer
Dim y As Integer
Dim v, w As Integer

Static Sub move_right()
  v = v + 10
  If v < 5350 Then
    Image1.Move v, w
  Else
    Image1.Move 2640, 1560
    v = 2640
    w = 1560
    Timer1.Enabled = False
  End If
End Sub

Public Sub Command1_Click(Index As Integer)
  Timer2.Enabled = False
  Timer3.Enabled = False
  Timer4.Enabled = False
  Timer1.Enabled = True
End Sub

Private Sub move_down()
  w = w + 10
  If w < 3670 Then
    Image1.Move v, w
  Else
    Image1.Move 2640, 1560
    v = 2640
    w = 1560
    Timer2.Enabled = False
  End If
End Sub

Private Sub move_left()
  v = v - 10
  If v > 0 Then
    Image1.Move v, w
  Else
    Image1.Move 2640, 1560
    v = 2640
    w = 1560
    Timer3.Enabled = False
  End If
End Sub

Private Sub move_up()
  w = w - 10
  If w > 0 Then
    Image1.Move v, w
  Else
    Image1.Move 2640, 1560
    v = 2640
    w = 1560
    Timer4.Enabled = False
  End If
End Sub

Private Sub Timer2_Timer()
  move_down
End Sub

Private Sub Timer3_Timer()
  move_left
End Sub

Private Sub Timer4_Timer()
  move_up
End Sub
VB.NET Animation Code
Public Class AnimationForm
    Private v As Integer = 2640
    Private w As Integer = 1560
    Private stepSize As Integer = 10
    
    Private Sub MoveRight()
        v += stepSize
        If v < Me.ClientSize.Width - Image1.Width Then
            Image1.Left = v
        Else
            ResetPosition()
            Timer1.Enabled = False
        End If
    End Sub
    
    Private Sub MoveDown()
        w += stepSize
        If w < Me.ClientSize.Height - Image1.Height Then
            Image1.Top = w
        Else
            ResetPosition()
            Timer2.Enabled = False
        End If
    End Sub
    
    Private Sub MoveLeft()
        v -= stepSize
        If v > 0 Then
            Image1.Left = v
        Else
            ResetPosition()
            Timer3.Enabled = False
        End If
    End Sub
    
    Private Sub MoveUp()
        w -= stepSize
        If w > 0 Then
            Image1.Top = w
        Else
            ResetPosition()
            Timer4.Enabled = False
        End If
    End Sub
    
    Private Sub ResetPosition()
        v = 2640
        w = 1560
        Image1.Left = v
        Image1.Top = w
    End Sub
    
    ' Button Click Handlers
    Private Sub btnRight_Click(sender As Object, e As EventArgs) Handles btnRight.Click
        Timer2.Enabled = False
        Timer3.Enabled = False
        Timer4.Enabled = False
        Timer1.Enabled = True
    End Sub
    
    Private Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click
        Timer1.Enabled = False
        Timer3.Enabled = False
        Timer4.Enabled = False
        Timer2.Enabled = True
    End Sub
    
    Private Sub btnLeft_Click(sender As Object, e As EventArgs) Handles btnLeft.Click
        Timer1.Enabled = False
        Timer2.Enabled = False
        Timer4.Enabled = False
        Timer3.Enabled = True
    End Sub
    
    Private Sub btnUp_Click(sender As Object, e As EventArgs) Handles btnUp.Click
        Timer1.Enabled = False
        Timer2.Enabled = False
        Timer3.Enabled = False
        Timer4.Enabled = True
    End Sub
    
    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        Timer1.Enabled = False
        Timer2.Enabled = False
        Timer3.Enabled = False
        Timer4.Enabled = False
    End Sub
    
    ' Timer Handlers
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        MoveRight()
    End Sub
    
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        MoveDown()
    End Sub
    
    Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
        MoveLeft()
    End Sub
    
    Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
        MoveUp()
    End Sub
End Class

VB6 Implementation

How the VB6 Code Works

  • The program uses four timers for each direction (up, down, left, right)
  • Each timer is associated with a movement procedure
  • Button clicks enable the corresponding timer and disable others
  • The Move method changes the position of the image
  • Boundary checking resets the position when edges are reached
  • Variables v and w store the current position

Note: In VB6, the coordinate system starts at (0,0) in the top-left corner, with X increasing to the right and Y increasing downward.

VB.NET Implementation

Key Differences in VB.NET

  • Uses Timer.Tick event instead of Timer_Timer
  • Object position controlled through Left and Top properties
  • More structured event handling with Handles clause
  • ClientSize property for boundary detection
  • Uses class-level variables with Private scope
  • ResetPosition method for better code organization

Tip: VB.NET provides better object-oriented structure and more precise control over UI elements.