How to Create a Clock in Visual Basic
Submitted by Yorkiebar on Monday, September 30, 2013 - 06:20.
Language
Introduction:
Welcome to a tutorial on how to create a clock in Visual Basic.
Steps of Creation:
Step 1:
I will just be loading the current time in to a simple, plain label. You can customize yours or set it as a filewriter etc.
So first we want to set checking for illegal crossThread calls to false on the form load. This is because we will be using a new thread to run the loop of updating the clock and it will be an illegal crossThread call which would throw an error. We also want to begin our function on form load.
We also want to import a threading namespace...
Step 2:
Next we want to create our begin function. This will create our new thread and set it to the addressOf updateClock, another custom function (which is not yet created). We also set it to background and start it.
Step 3:
Finally we create our updateClock function. This simply sets the label1 text with the current time in the format of HourHour:MinuteMinute:SecondsSeconds.
Project Complete!
Below is the full source code and download to the files...
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- CheckForIllegalCrossThreadCalls = False
- begin()
- End Sub
- Imports System.Threading
- Private Function begin()
- Dim trd As thread = New Thread(AddressOf updateClock)
- trd.isBackground = True
- trd.Start()
- End Function
- Private Function updateClock()
- While True
- Label1.Text = Format(Now, "hh:mm:ss")
- thread.sleep(1000)
- End While
- End Function
- Imports System.Threading
- Public Class Form1
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- CheckForIllegalCrossThreadCalls = False
- begin()
- End Sub
- Private Function begin()
- Dim trd As thread = New Thread(AddressOf updateClock)
- trd.isBackground = True
- trd.Start()
- End Function
- Private Function updateClock()
- While True
- Label1.Text = Format(Now, "hh:mm:ss")
- thread.sleep(1000)
- End While
- End Function
- End Class
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 87 views