0% found this document useful (0 votes)
13 views

LESSON7 - The TIMER Contro1

The document discusses the Timer control in Visual Basic, which allows performing tasks at intervals or waiting for a specified time. It describes Timer control properties and provides three examples: counting clicks of a button, making a label blink, and building a stopwatch app.

Uploaded by

polycarpkamolo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

LESSON7 - The TIMER Contro1

The document discusses the Timer control in Visual Basic, which allows performing tasks at intervals or waiting for a specified time. It describes Timer control properties and provides three examples: counting clicks of a button, making a label blink, and building a stopwatch app.

Uploaded by

polycarpkamolo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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. Note
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 Timer 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 True by default.

Timer Control Properties

Property Description
Name Defaults to Timer1 if you have one Timer control, Timer2 if you have two Timer
controls, and so on.
Property Description
Enabled Turns a Timer on and off. The default is True, or on.
Index Reports the position of a specific Timer control within a control array.
Interval Determines, in milliseconds, when a Timer event will fire (1 second = 1000).
Left The position of the left edge of a Timer.
Tag The Tag property is like having a variable built right into the control. It's a catch-all
property to which you can assign any necessary data to your program and that you want
to persist throughout the life of your program.
Top The position of the top edge of a Timer.
-

EXAMPLE1
a) Start a new VB project and place two command buttons and a timer control on the form. Set the properties of
the controls as follows:

Control Property Value


Command Button Name cmdStart
Caption Start Timer

Command Button (Name) cmdStop


Caption Stop Timer

Timer (Name) tmrTest


Enabled False
Interval 250

Your form should look something like this:


b) Place the following declaration in the Form's General Declarations section:

Private mintCount As Integer

c) Code the Click event for the two command buttons and the Timer event for the timer control as follows:

Private Sub cmdStart_Click()

mintCount = 0
Cls
tmrTest.Enabled = True

End Sub
Private Sub cmdStop_Click()
tmrTest.Enabled = False
End Sub

Private Sub tmrTest_Timer()


mintCount = mintCount + 1
Print "Timer fired again. Count = " & mintCount

End Sub

d) Run the program. Click the "Start Timer" button. Messages will appear on the form every quarter of a second.
They will stop when you click the "Stop Timer" button. A sample run might look like this:

Timer Example 2
The timer control
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 Visible property is available to nearly all
controls – it is a Boolean property that determines whether or not the control is visible to the user at run time by
setting its value to True or False (it will always be visible at design time). To build a simple demo, perform the
following steps.

 Start a new VB project.


 Place a label on the form and name it lblProcessing. Set its Caption to Processing . . . Please Wait. Set it
to a large font and set its Forecolor property to the color of your choice. Center it on your form.
 Place a timer on the form and name it tmrBlink. Set its Interval property to 250.
 In the tmrBlink_Timer event, place the following single line of code:

lblProcessing.Visible = Not lblProcessing.Visible

 Run the project. The label will blink. After an appropriate period of observation, close the form.

EXAMPLE 3: STOP WATCH PROJECT

e) Start a new VB project and place two command buttons and a timer control on the form. Set the properties of
the controls as follows:

Control Property Value


Form caption StopWatch
Style FixedSingle
StartPosition CentreScreen

Label Caption 00:00:00


Alignment Centre
Backcolour Black
Forecolour White
Font Courier New
Bold
Size 36
Command Button (Name) cmdStart
Caption Start

Command Button (Name) cmdStop


Caption Stop

Timer (Name) Timer1


Enabled False
Interval 1000
The interface should look as shown below

Attach the code as below

Dim starttime
Private Sub Command1_Click()
starttime = Now
Timer1.Enabled = True
End Sub

Private Sub Command2_Click()


Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Format$(Now - starttime, "hh:mm:ss")
End Sub

You might also like