Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Best way to implement a 'clock'

Posted on 2007-11-20
5
Medium Priority
?
197 Views
Last Modified: 2013-11-26
I've done this in vb6 - but wanted to know the best route to take in vb.net (2005).
I simply want a label with the current time HH:MM:SS to update every second.

If a timer is the best route, can someone assist me in writing the code for it - without placing the timer control on the form?
0
Comment
Question by:sirbounty
5 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 20322844
Go ahead and PLACE the timer control "on the form" - it is not visible, so it won't cause you any problem - it is just a way to indicate to the designer that you are using it!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20322846
you don't need the timer as a component. Have a look to this article about using the .net Timer() class:
http://www.codeproject.com/csharp/cstimer01.asp

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 20323834
A simple VB.Net version...
Public Class Form1
 
    Private WithEvents tmr As New System.Windows.Forms.Timer()
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tmr.Interval = 1000
        tmr.Start()
    End Sub
 
    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
        Label1.Text = DateTime.Now.ToString("hh:mm:ss")
    End Sub
 
End Class

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20323848
*Note that in VB.Net you can use the WithEvents/Handles combination...    (C# doesn't have a direct equivalent)
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20324758
Thanx IM - that's what I'd been trying to get working, but was missing something...
I think at one point it was complaining about tmr already existing in 'this' class, although I couldn't find any reference.
Will give this another shot - thanx!
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Screencast - Getting to Know the Pipeline
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question