Simple Clock in Visual Basic

Introduction: This tutorial is on how to create a simple clock in Visual Basic. Design: For this, we simply need a new Windows Form, with one label - call it; 'clockLbl'. You may center the label to the middle of the form in both x and y axis' and give it a large font size. We also need a timer, name this timer1. Set it's interval to 1000ms/1second and it's enabled state to 'True'. Form Load: On form load, we want to initiate the label with the current computer time. So first we get the current time and store it in to a new 'time' string variable...
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.     Dim time As String = DateTime.Now.ToLongTimeString().ToString()
  3. End Sub
Next we set the label text to the time variable's value...
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.     Dim time As String = DateTime.Now.ToLongTimeString().ToString()
  3.     ClockLbl.Text = time
  4. End Sub
And finally we start our timer1...
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.     Dim time As String = DateTime.Now.ToLongTimeString().ToString()
  3.     ClockLbl.Text = time
  4.     timer1.start()
  5. End Sub
Timer 1 Tick: The final thing we need to do is code our timer 1 tick event. This will simply re-set the text of the label to the now current time of the PC...
  1. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  2.     Dim time As String = DateTime.Now.ToLongTimeString().ToString()
  3.     ClockLbl.Text = time
  4. End Sub
Finished!:

Add new comment