In this article I will explain with an example, how to implement Timer in Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
The following Form consists of a Label and a Timer control.
The Timer control has been assigned with the following properties.
Enabled – This property enables the Timer control when set to True.
Interval – The interval (delay) after which Timer will tick in milliseconds. Here it is set to 1000 milliseconds i.e. one second.
Implementing Timer in Windows Forms using C# and VB.Net
When a second changes, following event handler is executed.
Inside this event handler, every second the Timer executes its OnTick event which in turns the Label is set with the current time.
C#
private void OnTick(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt");
}
VB.Net
Private Sub OnTick(sender As Object, e As EventArgs) Handles Timer1.Tick
lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt")
End Sub
Screenshot
Downloads