Solved

Best way to implement a 'clock'

Posted on 2007-11-20
5
186 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 85

Accepted Solution

by:
Mike Tomlinson earned 250 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 85

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…

679 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