Solved

Best way to implement a 'clock'

Posted on 2007-11-20
5
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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