In this article I will explain with an example, how to implement Digital Clock in Windows Forms (WinForms) Application using C# and VB.Net.
The Digital Clock will show both 12 hour and 24 hour time format.
 
 
Form Design
The following Form consists of two RadioButtons, a Label and a Timer control.
Digital Clock in Windows Forms using C# and VB.Net
 
RadioButton Control
The first RadioButton control has been assigned with the following property:
Checked – This property makes RadioButton checked or unchecked. Here it is checked.
This property has been assigned to the rbtn12 which enables twelve hours format.
Digital Clock in Windows Forms using C# and VB.Net
 
The RadioButton controls have been assigned with the following event:
CheckedChanged – This property checks whether the RadioButton is checked or unchecked.
Digital Clock in Windows Forms using C# and VB.Net
 
Digital Clock in Windows Forms using C# and VB.Net
 
Timer Control
The Timer control has been assigned with the following properties:
Enabled – This property enables the Timer control when set to True.
Digital Clock in Windows Forms using C# and VB.Net
 
Interval – The interval (delay) after which Timer will tick in milliseconds. Here it is set to 1000milliseconds i.e. one second.
Digital Clock in Windows Forms using C# and VB.Net
 
 
Implementing Digital Clock in Windows Forms using C# and VB.Net
The initial time format is set to 12 hour time format inside the timeFormat variable.
Then inside the Form Load event handler, the ShowTime method is called which sets the Current time in the Label control.
C#
string timeFormat = "hh:mm:ss";
private void Form1_Load(object sender, EventArgs e)
{
    this.ShowTime();
}
 
private void ShowTime()
{
    lblTime.Text = DateTime.Now.ToString(this.timeFormat);
}
 
VB.Net
Private timeFormat As String = "hh:mm:ss"
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    Me.ShowTime()
End Sub
 
Private Sub ShowTime()
    lblTime.Text = DateTime.Now.ToString(Me.timeFormat)
End Sub
 
 
OnTick
When the Timer control is ticked, the ShowTime method is called.
C#
private void OnTick(object sender, EventArgs e)
{
    this.ShowTime();
}
 
VB.Net
Private Sub OnTick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    Me.ShowTime()
End Sub
 
Note: For more details on Timer control in Windows Forms, please refer my article Implement Timer in Windows Forms using C# and VB.Net.
 
 
Switching the Clock Time Format
Inside the OnCheckChanged event handler of the RadioButton, based on the selection of the 12 hour or 24 hour RadioButton, the time format for the Digital Clock is set.
C#
private void OnCheckedChanged(object sender, EventArgs e)
{
    this.timeFormat = rbtn24.Checked ? "HH:mm:ss" : "hh:mm:ss tt";
}
 
VB.Net
 
Private Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbtn12.CheckedChanged
    Me.timeFormat = If(rbtn24.Checked, "HH:mm:ss", "hh:mm:ss tt")
End Sub
 
 
Screenshot
Digital Clock in Windows Forms using C# and VB.Net
 
 
Downloads