Lesson 32: Using Timer in VB2019
Create time-based applications with the Timer control
Key Takeaway
The Timer control in Visual Basic 2019 enables you to create applications that execute code at specified intervals, perfect for clocks, animations, and time-based processes.
After learning about coloring shapes in the previous lesson, we'll now explore how to create time-based applications using the Timer control in VB2019. The Timer control allows you to execute code at regular intervals, enabling you to create clocks, stopwatches, animations, and much more.
Interval Property
Controls the time between timer events (in milliseconds)
Enabled Property
Turns the timer on or off programmatically
Start Method
Activates the timer by setting Enabled to True
Stop Method
Deactivates the timer by setting Enabled to False
32.1 Timer Control Fundamentals
The Timer control in VB2019 is a hidden component at runtime that triggers the Tick event at specified intervals. The key properties are:
| Property | Description | Default Value |
|---|---|---|
| Interval | Time between timer events in milliseconds (1-65535) | 100 |
| Enabled | Determines whether the timer is active | False |
To use a timer:
- Drag the Timer control from the Toolbox onto your form
- Set the Interval property to the desired time (in milliseconds)
- Double-click the timer to create the Tick event handler
- Add your time-based code to the Tick event
32.2 Creating a Digital Clock
Let's create a digital clock that updates every second:
Example 32.1: Digital Clock
This example creates a digital clock that updates every second:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick ' Update the label with the current time LblClock.Text = TimeOfDay.ToString("hh:mm:ss tt") End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Configure the timer to trigger every 1000ms (1 second) Timer1.Interval = 1000 Timer1.Enabled = True End Sub
Digital Clock Demo
Updates every second
Figure 32.1: Digital clock application
32.3 Creating a Stopwatch
Let's build a stopwatch with start, stop, and reset functionality:
Example 32.2: Stopwatch
This example creates a stopwatch with millisecond precision:
Dim counter As Integer = 0 Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles BtnStart.Click Timer1.Enabled = True End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick counter += 1 ' Convert milliseconds to time format Dim ms = counter Mod 1000 Dim sec = (counter \ 1000) Mod 60 Dim min = (counter \ 60000) Mod 60 LblStopwatch.Text = $"{min:00}:{sec:00}.{ms \ 10:00}" End Sub Private Sub BtnStop_Click(sender As Object, e As EventArgs) Handles BtnStop.Click Timer1.Enabled = False End Sub Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click Timer1.Enabled = False counter = 0 LblStopwatch.Text = "00:00.00" End Sub
Stopwatch Demo
Figure 32.2: Stopwatch application
32.4 Creating a Digital Dice
Let's create a digital dice that simulates rolling with random numbers:
Example 32.3: Digital Dice
This example creates a dice that rolls when clicked:
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 ' Generate random number between 1 and 6 Dim diceNumber As Integer = random.Next(1, 7) LblDice.Text = diceNumber.ToString() rollCount += 1 ' Stop after 10 rolls If rollCount >= 10 Then Timer1.Enabled = False End If End Sub
Digital Dice Demo
Figure 32.3: Digital dice application
Lesson Summary
In this lesson, you've learned how to use the Timer control in VB2019 to create time-based applications:
Timer Fundamentals
Understand the Interval and Enabled properties
Digital Clock
Create a clock that updates every second
Stopwatch
Build a stopwatch with start, stop, and reset functionality
Digital Dice
Create a random dice simulator with animation
With timer controls mastered, you're ready to explore animation. In the next lesson, we'll learn to create animations using timers and graphics.
Next Lesson
Learn to create dynamic animations in Lesson 33: Creating Animation.
Related Resources
Visual Basic 2019 Made Easy
Master Visual Basic 2019 with this comprehensive guide that includes detailed coverage of timer controls. Learn to create professional time-based applications.
Key Timer Topics:
- Timer control fundamentals
- Creating digital clocks
- Building stopwatches
- Animation with timers
- Practical projects with full source code
Visual Basic Programming With Code Examples
This comprehensive guide includes numerous examples of timer applications in both VB6 and VB.NET, helping you understand the evolution of time-based programming in Visual Basic.
Timer Coverage:
- 15+ timer examples
- Step-by-step timer tutorials
- Advanced timer techniques
- Real-world timer applications