VB Timer

doantrang
doantrang used Ask the Experts™
on
How does the VB Timer work? I'm creating this calendar application in which I want to be able to time a person's appointment, meeting or the time they spend to work on a task. How do I start/stop the Timer and how do I show the time while they're doing their activities? I would so appreciate any code snippets!
Trang
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Project manager
Commented:
For using vb timer as chrono:

' Button to start chrono
Private Sub cmdStart_Click()
   tmrChrono.enabled = true
End sub

' Button to stop chrono
Private Sub cmdStart_Click()
   tmrChrono.enabled = false
End sub

' adjust timer and label
Private Sub tmrChrono_Timer()
   static secTeller as integer
   secTeller = secTeller + 1
   lblSec.caption = cstr(secteller)
End sub
Fisrt off, why use a timer for this kind of aplication. Why not just record the time when they start and then the time when they stop?

e.g
Public TimeStart AS Variant
Public TimeEnd AS Variant
Public TotTime AS Variant

Private Sub cmdStart_Click()
    TimeStart = Format(Now(),"hh:mm:ss")
End Sub

Private Sub cmdEnd_Click()
    TimeEnd = Format(Now(),"hh:mm:ss")
    TotTime = Format(CDate(TimeEnd) - CDate _
                (TimeStart), "hh:mm:ss")
End Private

Regards,

VSS
Hi,
If u using the timer for learining purpose then its very nice idea to learn your way.
But otherwise for your requirement you should not use timer.
Anyway ,first of all keep the timer setting to 1000 that is for one second
then timer is always enabled by default
so on form load u need to disable it then whenever required u can enable it by
Timer1.enable = true
and disable it by
Timer1.enable = false
now in the timers event Timer1_timer
write the code to increment the counter

& there u are when the timer is disabled by your code
then u can read the counter

also to show the time u can use the same counter
I propose u try to make an OCX which can be used after words in other applications also
Regards,
NILESH


Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

In addition to using the start/stop time as indicated by VoidStarStar, you could use the Timer function.  This can produce fractions of a second.

Timer Control on form as Timer1 with interval set to 1000
Label Control on form as Label1

dim sngElapsedTime as single
dim sngStartWorkTime as single

sub cmdStartTime_Click
  sngStartWorkTime = Timer
  Timer1.enabled = true
end sub

sub StopWorkTime_Click
   sngElapsedTime = timer() - sngStartWorkTime
   Timer1.Enabled = False
   label1.caption = "Timer stopped"
end sub

sub Timer1_Timer
   if sngStartWorkTime > 0 then
     Label1.caption = timer() - sngStartWorkTime & " Seconds elapsed"
   else
     Label1.caption = "Timer Not Started"
   endif
end sub
No comment has been added lately.
To cleanup this TA, I will recommend to moderators that this question is:

*** Answered by: VoidStarStar ***

Please leave any comments here within the next seven days, or finalize the question if you own it
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
peterchen
EE Cleanup Volunteer

My personal recomendation when the asker doesn't come back is to accept the first person's answer that addresses all of the concerns the questioner had.  That would be Dhaest's example.

VoidStarStar's comment does not satisfy the requirement for "and how do I show the time while they're doing their activities?", the example only shows the time at the end.

My recommendation would be:

- answered Dhaest
twalgrave: just my personal opinion:
I consider VoidVoidStar's comment technically far superior (Dhaest's is closer to the ideas of the asker, but VoidVoidStar's the "way to do it" IMO - and it should be marked as such for the PAQ DB)

Moderator: take your pick ;)
disregard my last comment - I must have been on drugs.
(sorry twalgrave)


changed recommendation:

*** Answered by: Dhaest ***

Please leave any comments here within the next seven days, or finalize the question if you own it

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

peterchen
EE Cleanup Volunteer
No prob at all, that's what the 7-day notif is all about!
Answered by Dhaest

SpideyMod
Community Support Moderator @Experts Exchange

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial