How to Show time elapsed using vb Timer?

saran_133
02-08-2005, 03:10 AM
Hello, I'm trying to make a app for a sports, that will show me the "time elapsed" using vb timer or time() function as Hour : Min : Second format. Time must start as 00:00:00. and again 00:00:00 when reset button clicked.

I searched whole internet for this, i couldn't find any proper answer for this.
I am a newbie, Please help. :whoops: :whoops: :cool:

Lintz
02-08-2005, 03:25 AM
To get the time passed use a Timer.

Track time passed and when it gets to 60 then 1 minute has passed.
Track time passed and when minutes get to 60 then 1 hour has passed.


Dim TimePassed As Long
Private Sub Form_Load()
Timer1_Timer
End Sub

Private Sub Timer1_Timer()
TimePassed = TimePassed + 1
Label1.Caption = "" & TimePassed & ""
End Sub

Azr@el
02-08-2005, 03:32 AM
For the vb Timer has a very low priority this way of a stopwatch would propably be unreliable.
i'd suggets you'd better use the GetTickCount-API (http://www.mentalis.org/apilist/GetTickCount.shtml). This gives you the number of milliseconds that elapsed since windows started. you would need to get the tickcount twice and then subtract the values to get the difference. You only need to convert it to a good looking time format and you have a very precise stopwatch ;)

Shatrix
02-08-2005, 04:15 AM
heres a quick example of how the code might look for a timer

Private Declare Function GetTickCount Lib "kernel32" () As Long 'declare the api
Dim s As Long 'The starting time
Dim sec As Integer 'seconds
Dim min As Integer 'minutes
Dim hour As Integer 'hours

Private Sub Form_Load()
Form1.AutoRedraw = True
s = GetTickCount 'set starting tim
Timer1.Interval = 10
End Sub

Private Sub Timer1_Timer()
Cls 'clear previous time
If GetTickCount > s + 1000 Then 'check for a seconds past
sec = sec + 1
If sec = 60 Then 'check for a minute
sec = 0
min = min + 1
If min = 60 Then 'check for an hour
hour = hour + 1
End If
End If
s = s + 1000
End If
Print hour & ":" & min & ":" & sec 'show total time
End Sub

done using the GetTickCount api :)

saran_133
02-08-2005, 08:51 AM
thank you verymuch, it helped a lot, I used MB hi Timer which i got free from internet. microsoft timer is not so correct i think.
thanks a lot again ;)

HardCode
02-08-2005, 10:15 AM
Are you trying to find the time elapsed for a section of your code? Or is the time elapsed going to include idle time when the app is open but a user is not interacting with the form? If you need time elapsed for a section of code, you don't need a Timer at all.

saran_133
02-08-2005, 10:49 AM
???

HardCode
02-08-2005, 11:04 AM
In other words, what are you trying to time?

a.) The time it takes for specific code to run?

b.) How long the program is open or being used by the user?

The Timer control is primarily used to kick off some code, not keep track of time that has passed to "see how long" something takes.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum