See Also Example1 Example2
Applies To
Timer.
Description
Occurs when a preset interval for a timer control has elapsed. The interval's frequency is stored in the control's Interval property, which specifies the length of time in milliseconds.
Syntax
Sub ctlname_Timer ([Index As Integer])
Remarks
The argument Index uniquely identifies a control if it is in a control array Use this procedure to tell Visual Basic what to do at every timer Interval. When you are working with the Timer event:
The Interval property specifies the interval between Timer events in milliseconds.
Whenever the timer control's Enabled property is set to True and the Interval is greater than zero, the Timer event waits for the period specified in the Interval property.
Help:
Enabled Property
Interval Property
Timer Function
Programmer's Guide:
Chapter 17, "Interacting with the Environment"
The example demonstrates a digital clock. To try this example, paste the code into the Declarations section of a form that contains a label and a timer control. Then press F5.
Sub Form_Load ()
Timer1.Interval = 1000 ' Set timer interval.
End Sub
Sub Timer1_Timer ()
Label1.Caption = Time ' Update time display.
End Sub
This example moves a picture box across a form. To try this example, paste the code into the Declarations section of a form that contains a timer and a picture control. Then press F5. For a better visual effect you can assign a bitmap to the picture box using the Picture property.
Dim DeltaX, DeltaY As Integer ' Declare variables.
Sub Timer1_Timer ()
Picture1.Move Picture1.Left + Deltax, Picture1.Top + DeltaY
If Picture1.Left < ScaleLeft Then DeltaX = 100
If Picture1.Left + Picture1.Width > ScaleWidth + ScaleLeft Then
DeltaX = -100
End If
If Picture1.Top < ScaleTop Then DeltaY = 100
If Picture1.Top + Picture1.Height > ScaleHeight + ScaleTop Then
DeltaY = -100
End If
End Sub
Sub Form_Load ()
Timer1.Interval = 1000 ' Set Interval.
DeltaX = 100 ' Initialize variables.
DeltaY = 100
End Sub