Lesson 32

Using Timer

The Timer control allows you to create time-based applications such as clocks, stopwatches, and animations.

32.1 Introduction

The Timer is a useful control in Visual Basic 2015 for creating applications that depend on time. Examples include clocks, stopwatches, dice games, and animations.

The Timer works behind the scenes and triggers events at fixed intervals.

32.2 Creating a Digital Clock

To create a digital clock:

  • Add a Timer control
  • Set Interval = 1000 (1 second)
  • Set Enabled = True
  • Add a Label to display time

Use the following code:

Private Sub Timer1_Tick(...) Handles Timer1.Tick

LblClock.Text = TimeOfDay

End Sub

TimeOfDay is a built-in function that returns the current system time.

Figure 32.1 – Digital Clock

32.3 Creating a Stopwatch

To create a stopwatch, use buttons to control the Timer.

Private Sub BtnStart_Click(...) Handles BtnStart.Click
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(...) Handles Timer1.Tick
    LblPanel.Text = Val(LblPanel.Text) + 1
End Sub

Private Sub BtnStop_Click(...) Handles BtnStop.Click
    Timer1.Enabled = False
End Sub

Private Sub BtnReset_Click(...) Handles BtnReset.Click
    LblPanel.Text = 0
End Sub

Figure 32.2 – Stopwatch

32.4 Creating a Digital Dice

To simulate a dice, generate random numbers using:

n = Int(1 + Rnd() * 6)

Set Timer interval to a small value (e.g. 10ms) to create a rolling effect.

Example logic:

Dim n, m As Integer

Private Sub Timer1_Tick(...) Handles Timer1.Tick

m = m + 10
n = Int(1 + Rnd() * 6)

LblDice.Text = n

If m > 1000 Then
    Timer1.Enabled = False
    m = 0
End If

End Sub

Figure 32.3 – Digital Dice

Build on This Foundation

Continue to VB2026

After learning the basics of checkbox controls in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 →

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Key Takeaways:
  • Timer triggers events at intervals
  • Interval is measured in milliseconds
  • Used for clocks, stopwatches, animations
  • Enable/Disable controls execution

Next: Creating Animation

Go to Lesson 33 →