Solved

Timer

Posted on 2002-03-06
8
286 Views
Last Modified: 2012-05-04
Hello All,

I need to display a clock value in a label that starts at 00:00:00 and increases upward using a timer control. When a hit a button it goes back to zero (00:00:00 and starts over. Any ideas?

Thanks ADawn
0
Comment
Question by:ADawn
8 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6846138
You need a Date Time/ Long int Value, maybe GetTickCount API, format the Value into 00:00:00 use Timer to display result only ..
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6846166
'Add a timer(Timer1), label (label1) & button (command1) button

Option Explicit
Private lTlSecs As Long

Private Sub Timer1_Timer()
Dim nHrs As Integer
Dim nMins As Integer
Dim nSecs As Integer
Dim nWk As Integer
lTlSecs = lTlSecs + 1
'be sure to use integer divide symbol \ not /
nHrs = lTlSecs \ 3600
nWk = lTlSecs Mod 3600
nMins = nWk \ 60
nWk = nWk Mod 60
nSecs = nWk
Label1.Caption = CStr(nHrs) & ":" & Format$(nMins, "00") & ":" & Format$(nSecs, "00")
End Sub

Private Sub Command1_Click()
'reset timer
lTlSecs = 0
End Sub

0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6846172
OOPS! Forgot to tell you to set Timer1.Interval = 1000
0
 
LVL 18

Accepted Solution

by:
deighton earned 25 total points
ID: 6846724
'WITH A TEXT BOX TEXT1, command buttons cmdstart and cmdreset + a timer timer1.





Option Explicit



Private Sub cmdReset_Click()

    Text1.Tag = CStr(Now)
    increment
   
End Sub

Private Sub cmdStart_Click()

    Timer1.Enabled = True
    Timer1.Interval = 1000
    Text1.Tag = CStr(Now)
    increment

End Sub

Private Sub Form_Load()

    Timer1.Enabled = False
   
End Sub

Sub increment()

    Text1 = Format(Now - CDate(Text1.Tag), "hh:mm:ss")

End Sub

Private Sub Timer1_Timer()

    increment
   
End Sub
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 4

Expert Comment

by:trkcorp
ID: 6847500
deighton, pretty elegant! I like that.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6847647
Just be aware that the thread priority of timer controls is lower than your average processes thread priority so, if your user is doing anything processor intensive during the timing, you probably wont get the Timer event to fire.  Or, it might fire inconsistently.  You'll still get the right elapsed time, it's just that you might not see the label update every second as you'd like.
0
 

Author Comment

by:ADawn
ID: 6847675
deighton
I like the simplicity of this code.

trkcorp
Your code work as expected, but much more complicated for me to understand.

Thanks to you both. ADawn
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6847828
ADawn, deighton's code is better and more accurate.  As mdougan points out, if there are other processes in your program interfering with the firing of the timer event, my timer code could become quite inaccurate as it may not fire every second on the second in this circumstance.  Deighton's approach, using current time as a baseline and getting the difference between that and NOW will always report an accurate lapse of time even if the timer event fails to fire at precisely every second.  Good choice, just wish I'd thought of it!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

20 Experts available now in Live!

Get 1:1 Help Now