vbnet
  1. vbnet-timer-control

Timer Control in VB.NET

The Timer control in VB.NET is used to execute a block of code at a set interval repeatedly. It can be useful in situations where you want to update the UI or perform some task after a certain amount of time has elapsed.

Syntax

To use the Timer control, you need to first add it to your form. You can then set the interval property to specify the time in milliseconds between each tick event.

' To add a Timer control to your form
Dim WithEvents Timer1 As New Timer

' Set the interval property
Timer1.Interval = 1000 ' 1000ms (1 second)

You also need to add an event handler for the Tick event, which is raised each time the interval elapses.

' Add an event handler for the Tick event
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    ' Code to execute on each tick event
End Sub

To start the timer, you can call the Start method. To stop the timer, you can call the Stop method.

' Start the Timer
Timer1.Start()

' Stop the Timer
Timer1.Stop()

Example

Public Class Form1
    Dim WithEvents Timer1 As New Timer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Set the interval property
        Timer1.Interval = 1000 ' 1000ms (1 second)
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ' Update the label with the current time
        Label1.Text = TimeOfDay.ToString("hh:mm:ss tt")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Start the Timer
        Timer1.Start()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' Stop the Timer
        Timer1.Stop()
    End Sub
End Class

Output

When you run the above example and click the "Start" button, the label control will update with the current time every second. When you click the "Stop" button, the timer will stop and the label will no longer update.

Explanation

In the above example, we have created a Timer control called Timer1 and set its interval property to 1000ms (1 second) in the Form Load event. We have also added a Tick event handler that updates the label control with the current time.

We have added two buttons - a Start button and a Stop button - that call the Start and Stop methods of the timer, respectively.

Use

The Timer control can be used to execute a block of code at a set interval repeatedly, which can be useful in situations where you want to update the UI or perform some task after a certain amount of time has elapsed.

For example, you could use the Timer control to periodically check for new messages in a messaging app and update the UI with any new messages that have arrived.

Important Points

  • The Timer control in VB.NET can be used to execute a block of code at a set interval repeatedly.
  • You can set the interval property to specify the time in milliseconds between each tick event.
  • You need to add an event handler for the Tick event, which is raised each time the interval elapses.
  • To start the timer, you can call the Start method. To stop the timer, you can call the Stop method.

Summary

In summary, the Timer control in VB.NET can be useful in situations where you want to execute a block of code at a set interval repeatedly. You can set the interval property to specify the time in milliseconds between each tick event, and add an event handler for the Tick event. You can start and stop the timer using the Start and Stop methods, respectively.

Published on: