• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Simple Clock in Visual Basic

RUoFx

Stealth Master
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
38
Likes
198
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
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!:

 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,500

351,032

351,042

Top