Visual Basic 2010 Lesson 27 – Using Timer

[Lesson 26] << [CONTENTS] >> [Lesson 28]

In this lesson, we shall show you how to use the timer in Visual Basic 2010. The timer is used to manage events that are time-related. For example, you can use the timer to create a clock, a stopwatch, a dice, animation and more.

27.1 Creating a Clock

To create a clock, you need to use the Timer control that comes with Visual Basic 2010. The Timer control is a control object that is only used by the developer, it is invisible during runtime and it does not allow the user to interact with it.

First of all, start a new project in Visual Basic 2010 and select a new Windows Application. You can give the project any name you wish, we named it MyClock. Change the caption of the Form1 to MyClock in the properties window. Now add the Timer control to the form by dragging it from the control tool Box. Next, insert a label control into the form. Change the Font size of the label to 14 or any size you wish, and set the Font alignment to be the middle center. Lastly, you shall also set the Interval property of the Timer control to 1000, which reflects a one-second interval(1 unit is 1 millisecond).




The statement to create a clock is only a one-line code, as follows:

Label1.Text = TimeOfDay

To create the clock, click on the Timer control and insert the code above, as shown below:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
 Label1.Text = TimeOfDay
End Sub

The resulting Clock is shown below:



27.2 Creating a Stopwatch

We can create a simple stopwatch using the Timer control. Start a new project and name it stopwatch. Change the Form1 caption to Stopwatch. Insert the Timer control into the form and set its interval to 1000 which is equal to one second. Besides, set the timer Enabled property to False so that it will not start ticking when the program is started. Insert three command buttons and change their names to StartBtn, StopBtn and ResetBtn respectively. Change their text to “Start”, “Stop” and “Reset” accordingly. Now,  key in the code as follows:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'To increase one unit per second
 Label1.Text = Val(Label1.Text) + 1
End Sub

Private Sub StopBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopBtn.Click
'To stop the Timer
 Timer1.Enabled = False
End Sub

Private Sub StartBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBtn.Click
'To start the Timer
 Timer1.Enabled = True
End Sub

Private Sub ResetBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetBtn.Click
'To reset the Timer to 0
Label1.Text = 0
End Sub

The Interface of the Stopwatch is as shown below:

 

27.3 Creating a Digital Dice

To create a dice, you need to generate random numbers using the Rnd function. Rnd generates numbers between 0 and 1. The statement

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

generates integers from 1 to 6 randomly.

In the code, we introduce the variable m to control the length of time of the rolling process. If m is more than 1000, then the rolling process will stop by setting the timer enabled property to False.

The complete code is shown below:

Public Class Form1
Dim n, m As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
 m = m + 10
If m < 1000 Then
 n = Int(1 + Rnd() * 6)
 LblDice.Text = n
Else
 Timer1.Enabled = False
 m = 0
End If
End Sub

Private Sub RollDiceBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RollDiceBtn.Click
 Timer1.Enabled = True
End Sub
End Class

Running the program produces a dice with fast changing numbers which stop at a certain number. The interface is shown below:

Visual Basic 2010


[Lesson 26] << [CONTENTS] >> [Lesson 28]