Solved

Best way to implement a 'clock'

Posted on 2007-11-20
5
184 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
*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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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 …
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now