The Timer Control
The Timer control allows you to perform a task at a specified interval or to wait for a specified length of time. Consider the following facts about the Timer control:
· It is not visible to the user at run-time
· The actions that you want to happen at the specified interval are coded in the Timer's Tick event.
· You specify the interval that you want actions to occur in the Timer control's Interval property. The value is specified in milliseconds (1000 milliseconds = 1 second). In the example, the value of 250 will cause the Timer event to fire every quarter of a second.
· You turn the timer "on" or "off" by setting its Enabled property to True or False, respectively. The Enabled property is set to False by default.
Timer Example 1
This sample projects writes messages to a multi-line textbox whenever the Timer's Tick event is fired.
The following controls were placed on the form and their properties set as follows: |
The design-time form is shown below. Note that you can change the height of a multi-line textbox. Note also that the Timer control appears in a gray tray area below the form. Some controls (such as the Timer) are not visible at run-time. Controls that are not visible at run-time appear in this gray tray area.
|
|||||||||||||||||||||||||||||||||||||||||||||
|
The code for the program is as follows:
Public Class Form1
Private mintCount As Integer = 0
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
tmrTest.Enabled = True
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
tmrTest.Enabled = False
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
tmrTest.Enabled = False
mintCount = 0
txtMessages.Text = ""
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub tmrTest_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTest.Tick
mintCount += 1
If txtMessages.Text <> "" Then txtMessages.Text &= ControlChars.NewLine
txtMessages.Text &= "Timer fired again. Count = " & mintCount
txtMessages.SelectionStart = Len(txtMessages.Text)
txtMessages.ScrollToCaret()
End Sub
The code the Start and Stop button events turn the timer on and off, respectively. The Reset button returns the program to its initial state. In the Timer's Tick event, a counter is incremented and a message is written to the multi-line textbox. The code there also ensures that we always see the most recent message written there by automatically scrolling to the end of the textbox text as the textbox fills up with messages.
A screen shot of the run is shown below:
Download the VB project code for the example above here.
Timer Example 2
The Timer control can be used to make an object on your screen appear to blink. This is done by toggling the Visible property of the object on and off at short intervals. The sample project below uses a Label named lblProcessing (Font set to Arial 26 Bold Italic, ForeColor set to Dark Red, and Text set to "Processing … Please Wait") and a Timer named tmrBlink with its Interval property set to 250 and Enabled set to True:
The Timer's Tick event contains a single line of code:
lblProcessing.Visible = Not lblProcessing.Visible
When you run the project, you will see that the label will blink.
Download the VB project code for the example above here.