Visual Basic Timer

The Visual Basic Timer control helps in executing a set of code repeatedly at specific intervals. When you draw the control on the form, it is only visible during design time and is hidden during runtime.

Tick Event

The code that needs to be executed is written in the Tick event of the Timer control in VB.NET and the same is known as Timer event in VB 6.0. This event is triggered after each Interval, as long as the Timer Enabled property is True.

Interval Property

This property of the visual basic timer control is used to set the number of milliseconds between subsequent Tick events (Timer event in case if VB 6.0). The value of this property is a 32-bit integer i.e. from 1 to 2147483647 - in VB.NET. In case if VB 6.0 it is a 16-bit integer and valid values are from 0 to 65535. If the value is set to 0 it means nothing will happen, the Timer event will not at all be triggered.

Enabled Property

This property is similar in use as for the other controls. When Enabled is set to True the code under the Tick event will keep getting executed after every elapse of Interval milliseconds. If you no longer want the Visual Basic Timer control to execute the Tick code, you need to stop the Timer by setting its Enabled property to False. In VB 6.0 this can also be done by setting the Interval property to 0.

Example

In this VB.NET example, we will use the visual basic timer control to create a count-down display on the Form. Start by creating a new VB.NET project with a Form. Place a label called lbl_CountDisplay on the Form, also place the Timer component on the Form and name it tmr_Countdown. Next set the Timer's Interval property to 1000 (1 second), set the Enabled property to true. Write the code as described below'Create a Form-level integer variable iCountdown
Dim iCountdown As Integer

'Write the following code on Form Load event
iCountdown = 10
lbl_CountDisplay.Text = iCountdown

'Write the following code on Visual Basic Timer Tick event
iCountdown = iCountdown - 1
lbl_CountDisplay.Text = iCountdown
If iCountdown = 0 Then tmr_Countdown.Enabled = False

Quick Summary

  • The VB Timer control is not visible to the user at run-time
  • The actions that you want to happen at the specified interval are coded in Timer's Tick event (Timer event in case of VB6.0).
  • 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).
  • You turn the Timer "on" or "off" by setting its Enabled property to True or False. By default the Enabled property is set to True in VB 6.0 and is False in VB.NET.

Visual Basic Timer - 6.0 and .NET Conceptual Differences

  • In Visual Basic 6.0, the Timer control is an actual control that is drawn on a form at design time; however it is not visible at run time.
  • In Visual Basic .NET, the Timer is a component that is added to the tray at design time; as a component it has no Parent property.
  • In Visual Basic .NET, the lowest value for the Interval property is 1 and you use the Enabled property to enable/disable its functionality.
  • In Visual Basic 6.0, you can disable a Visual Basic Timer control by setting the Interval property to 0.
Back to Visual Basic Help