• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

Time on a Form

I would like to display the time on a form
which updates in real time as long as the form
is open.
Can someone show me how ?
0
kennys
Asked:
kennys
1 Solution
 
MarineCommented:
put the code in a timer and just use Time function. This can work . Set its interval to 1.

and set a variable or soemthing or a label which will show this.
Private Sub Timer1
timer1.interval = 1
Lable1.Caption = Time()
End Sub
0
 
Erick37Commented:
Put a Timer control and a Label control on your form:

Option Explicit

Private Sub Form_Load()
    Timer1.Interval = 300
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Label1.Caption = Format(Time, "hh:nn:ss AM/PM")
End Sub
0
 
PatrickVDCommented:
Hi Kennys,

Basically, you can do this very simple.
All you need is a Timer control and a label on the form you want to show the time on. (It might be interesting to encapsulate this in a custom control in order to reuse this easely on different forms...)

Here's what you need to do to show Time on a form.
1. Create a new Form
2. put a Label on it (resize to fit the time in there) and change the name to 'TimeLabel'
3. put a timer control on the form and set its name to 'TimeUpdater'. Make sure to set the Enabled property to 'True' and the interval = 1000.
4. In the TimeUpdater_Timer event(assuming that the name of the timer control is 'TimeUpdater'), you simply add the following code:

Private Sub TimeUpdater_Timer()

   TimeLabel.Caption = Format(Now,"Hh:Nn:Ss")
   DoEvents

End Sub


That's all...

Hope this is what you're looking for !
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PatrickVDCommented:
Sorry guys... It seems I was typing my solution at the same time as the others...

Sorry.
0
 
Erick37Commented:
I guess the only point of difference is if you want 24 hour time or AM/PM time.

Label1.Caption = Time
or
Label1.Caption = Format(Time, "hh:nn:ss AM/PM")
both return AM/PM time

While
Label1.Caption = Format(Time, "hh:nn:ss")
will display 24 hour time.

Another point:  The timer has a minimum resolution of 55 (milliseconds).  Set your timer Interval to be less than 1000 (1 second) ans greater than 55.  I used 300, which updates the display every 0.3 seconds.  Anything faster is not needed and will take more CPU time to update.

You could also update the Label only when the time changes like this:

Private Sub Timer1_Timer()
    Static tPrevTime
    If (tPrevTime <> Time) Then
        Label1.Caption = Format(Time, "hh:nn:ss")
        tPrevTime = Time
    End If
End Sub
0
 
wsh2Commented:
What is with setting the Timer interval to 300?.. ie.. 3 Events per second, 2 of which we already know will go wasted. Erik37's comment of a possible 55 millisecond error is a good one.. therefore we should set our interval to (1000 (one second) - 55) - 1 = 944.. to avoid wasting cycles and to still maintain accuracy.. <smile>.

By the way.. (although this is far from being the best solution).. the Statusbar control contains builtin clock support.. <duh, way tago WSH2.. LOL>.
0
 
amebaCommented:
If your app is busy,
you'll need < 900 ms period, just in case... Also, add 'Label1.Refresh' for immediate update
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
If you have a Statusbar control already on your form, you may define one of the panels to display the time (or the date). The time is updated automatically.
If you don't want that control, use the proposed solutions above
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now