Visual Basic 2013 Lesson 31: Using Timer

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

[Lesson 30] << [Contents] >> [Lesson 32]

31.1 What is Timer?

Timer is a useful control in Visual Basic 2013. It can be used to write code for events that are time related. For example, you can use timer to create a clock, a stop watch, a dice, an animated application and more. Timer is a hidden control at runtime, like the engine of an automobile. We shall illustrate the usage of timer using a few examples.

31.2 Creating a Digital Clock

To create the clock, first of all start a new project in Visual Basic 2013 Express and select a new Windows Application. You can give the project any name you wish, but we will name it MyClock. Change the text of the Form1 to MyClock in the properties window.

Now add the Timer control to the form by double-clicking it in the ToolBox. Next, insert a label control into the form. Change the Font size of the label to any size you wish, and set the Font alignment to be middle center. Before we forget, you shall also set the Interval property of the Timer control to 1000, which reflects a one second interval(1 unit is 1 millisecond). Remember to set the MaximizeBox property of Form1 to false so that the user cannot enlarge the clock. You also need to ensure that the Enabled property of the Timer control is set to True so that the clock starts running as soon as it is loaded.



Now, you are ready for the coding. Actually you would be surprise that what you need to create a clock is only a one-line code, that is:

Label1.Text = TimeOfDay

*TimeOfDay() is a Visual Basic 2013 function that returns the current time today based on your computer system time.

Click on the Timer control and enter the code above ,as shown below:

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

The digital clock is as shown in Figure 31.1

vb2013_figure31.1Figure 31.1




 31.3 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 that, set the timer Enabled property to False so that it will not start ticking when the program is started. Insert three  buttons and change their names to BtnStart, BtnStop and BtnReset respectively. Change their text to “Start”, “Stop” and “Reset” accordingly. Now, enter the code as follows:

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
 LblPanel.Text = Val(LblPanel.Text) + 1
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
 LblPanel.Text = 0
End Sub

The Interface of the Stopwatch is as shown in Figure 31.2

vb2013_figure31.2Figure 31.2

 31.4 Creating a Digital Dice

We can create a digital dice easily using the Timer Control. To create a dice, you need to generate random numbers using the Rnd function. Rnd generates numbers between 0 and 1. The following statement generates random integers from 1 to 6 

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

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. Set the timer interval to 10 so that the number changes every 0.01 second.

The Code

Public Class Form1
Dim n, m As Integer
Private Sub Timer1_Tick(sender As Object, e As 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 BtnRoll_Click(sender As Object, e As EventArgs) Handles BtnRoll.Click
 Timer1.Enabled = True
End Sub
End Class

Running the program produces a dice with fast changing numbers which stops at a certain number. The interface is as  shown in Figure 31.3

vb2013_figure31.3

 Figure 31.3





[Lesson 30] << [Contents] >> [Lesson 32]

 

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone