Lesson 33: Creating Animation in VB2019

Bring your applications to life with dynamic animations using Timer controls

Key Takeaway

Combine Timer controls with graphics programming to create dynamic animations that enhance user experience and visual appeal in VB2019 applications.

Building on our knowledge of Timer controls from the previous lesson, we'll now explore how to create captivating animations in VB2019. Animation brings your applications to life, making them more engaging and visually appealing.

Motion Animation

Move objects across the screen using position properties

Graphical Effects

Create dynamic graphics that change over time

Interactive Elements

Build game-like interfaces with animated components

Refresh Method

Update visual elements to create smooth animations

33.1 Animation Fundamentals

Animation in VB2019 involves changing an object's properties over time. The key principles are:

Technique Description Common Properties
Position Animation Moving objects across the screen Left, Top, Location
Size Animation Changing object dimensions Width, Height, Size
Visual Animation Altering appearance over time BackColor, ForeColor, Image
Graphic Animation Drawing dynamic graphics Graphics methods (DrawEllipse, etc.)

To create smooth animations:

  1. Set the Timer's Interval property appropriately (30-100ms works well)
  2. Use the Timer's Tick event to update object properties
  3. Call the Refresh() method to update the display
  4. Manage animation state with boolean flags or enumerations

33.1 Creating Motion Animation

Let's create an animation where an image moves across the screen:

Example 33.1: Moving Image

This example creates a horizontally moving image that loops across the form:

Form1.vb
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    ' Move the picture box 10 pixels to the right
    If PictureBox1.Left < Me.Width Then
        PictureBox1.Left = PictureBox1.Left + 10
    Else
        ' Reset position when it reaches the edge
        PictureBox1.Left = 0
    End If
End Sub

Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles BtnStart.Click
    Timer1.Enabled = True
End Sub

Private Sub BtnStop_Click(sender As Object, e As EventArgs) Handles BtnStop.Click
    Timer1.Enabled = False
End Sub

Motion Animation Demo

Grapes
Moving image on form

Figure 33.1: Motion animation application

33.2 Creating a Graphical Dice

Let's build an animated dice that rolls when clicked:

Example 33.2: Graphical Dice

This example creates a dice that displays random faces when rolled:

Form1.vb
Dim rollCount As Integer = 0
Dim random As New Random()

Private Sub BtnRoll_Click(sender As Object, e As EventArgs) Handles BtnRoll.Click
    rollCount = 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    MyPicBox.Refresh()
    
    Dim n As Integer = random.Next(1, 7)
    Dim myPen As New Pen(Color.DarkTurquoise, 10)
    Dim g As Graphics = MyPicBox.CreateGraphics
    
    ' Draw dice face based on random number
    Select Case n
        Case 1
            g.DrawEllipse(myPen, 80, 80, 10, 10)
        Case 2
            g.DrawEllipse(myPen, 40, 40, 10, 10)
            g.DrawEllipse(myPen, 120, 120, 10, 10)
        ' Cases 3-6 would be implemented similarly
    End Select
    
    rollCount += 1
    If rollCount >= 15 Then
        Timer1.Enabled = False
    End If
End Sub

Graphical Dice Demo

Graphical dice on form

Figure 33.2: Graphical dice application

33.3 Creating a Slot Machine

Let's create an animated slot machine with three spinning reels:

Example 33.3: Slot Machine

This example creates a slot machine with three spinning reels:

Form1.vb
Dim m, a, b, c As Integer
Dim random As New Random()

Private Sub BtnSpin_Click(sender As Object, e As EventArgs) Handles BtnSpin.Click
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    m += 1
    
    If m < 50 Then
        ' Generate random fruits for each reel
        a = random.Next(1, 4)
        b = random.Next(1, 4)
        c = random.Next(1, 4)
        
        ' Update images based on random values
        UpdateReel(PictureBox1, a)
        UpdateReel(PictureBox2, b)
        UpdateReel(PictureBox3, c)
    Else
        Timer1.Enabled = False
        m = 0
        CheckResult(a, b, c)
    End If
End Sub

Private Sub UpdateReel(pb As PictureBox, fruit As Integer)
    Select Case fruit
        Case 1
            pb.Image = Image.FromFile("strawberry.jpg")
        Case 2
            pb.Image = Image.FromFile("grape.jpg")
        Case 3
            pb.Image = Image.FromFile("apple.jpg")
    End Select
End Sub

Slot Machine Demo

🍓
🍇
🍎
Slot machine on form

Figure 33.3: Slot machine application

Lesson Summary

In this lesson, you've learned how to create various types of animations using Timer controls in VB2019:

Motion Animation

Moving objects across the screen by updating position properties

Graphical Animation

Creating dynamic graphics that change over time

Interactive Elements

Building game-like interfaces with animated components

Slot Machine

Creating a complex animation with multiple synchronized elements

With animation techniques mastered, you're ready to explore databases. In the next lesson, we'll learn to create database applications in VB2019.

Next Lesson

Learn to create database applications in Lesson 34: Database Introduction.

Related Resources

VB2019 Animation Guide

Comprehensive guide to animation in VB2019

Explore Guide

Animation Sample Code

Practical VB2019 code samples for animations

View Examples

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon

VB2019 Kindle Edition

Digital version of Visual Basic 2019 Made Easy

Get on Kindle