VB2019 VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Code 中文VB About Us

Lesson 28: Using the Timer



28.1 Creating a Clock

To create a clock, you need to use the Timer control that comes with Visual Basic 2008. 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 2008 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:

LblClock.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
 LblClock.Text = TimeOfDay
End Sub

The resulting Clock is shown in Figure 28.1

Figure 28.1

28.2 Creating a Stopwatch

We can create a 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 100 which is equal to 0.1 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 BtnStart, BtnStop and BtnReset respectively. Change their text to “Start”, “Stop” and “Reset” accordingly.

Secondly, declare three variables t, s and m. Variable t is for displaying tenth seconds, s to display seconds and m to display minutes. Under the Form load event, enter the following code:

LblDisplay.Text = "00" + ":" + "00" + ":" + "00"

so that the stopwatch display the 00:00:00 format on start up.

Next, create a sub procedure displaytime() and key in the following code:

Sub displaytime()

LblDisplay.Text = Format(m, "00") + ":" + Format(s, "00") + ":" + Format(t, "00")

End Sub

*The Format function ensures the number is displayed in a two-digit format

Now, under the Timer1 procedure key in the code as follows:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
 t = t + 1
 displaytime()

 If t > 10 Then
  t = 0
  If s < 60 Then
   s = s + 1
  Else
   s = 0
   m = m + 1
  End If

  End If

End Sub
We use a nested If statements to control the tenth second output, the second output and the minute output.

The full code is as shown below:

Public Class Form1
    Dim t As Integer
    Dim s As Integer
    Dim m As Integer


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

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

    Private Sub BtnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReset.Click
        'To reset the Timer to 0

        LblDisplay.Text = "00" + ":" + "00" + ":" + "00"
        t = 0
        s = 0
        m = 0

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        t = t + 1
        displaytime()

        If t > 10 Then
            t = 0
            If s < 60 Then
                s = s + 1
            Else
                s = 0
                m = m + 1
            End If

        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        LblDisplay.Text = "00" + ":" + "00" + ":" + "00"
    End Sub



    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Sub displaytime()

        LblDisplay.Text = Format(m, "00") + ":" + Format(s, "00") + ":" + Format(t, "00")

    End Sub
    
End Class

The Interface of the Stopwatch is as shown in Figure 28.2

Figure 28.2

28.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 shownin Figure 27.3

Visual Basic 2010

Figure 28.3


❮ Previous Lesson Next Lesson ❯

Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy