Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Timer

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
ADawn
Asked:
ADawn
1 Solution
 
Ryan ChongCommented:
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
 
trkcorpCommented:
'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
 
trkcorpCommented:
OOPS! Forgot to tell you to set Timer1.Interval = 1000
0
Industry Leaders: 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!

 
deightonCommented:
'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
 
trkcorpCommented:
deighton, pretty elegant! I like that.
0
 
mdouganCommented:
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
 
ADawnAuthor Commented:
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
 
trkcorpCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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