Link to home
Start Free TrialLog in
Avatar of n-generation
n-generation

asked on

Producing a Stop watch.

I want to produce a stopwatch, (i.e. a timer) able to display minutes, seconds, miliseconds.  How will I get round doing this?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vinnyd79
vinnyd79

actually doesn't look like that example handles milliseconds. You could try this link under downloads:

http://www.mvps.org/ccrp/

There is a hi-performance timer control.
Put the following declaration in a module:

Public Declare Function GetTickCount Lib "kernel32" () As Long

On your form, place a timer control (Timer1) with an interval of 1 (ie, 1 millisecond), and a label (Label1).

Private Sub Timer1_Timer()
    Static lngStart As Long
    Dim ms As Long
    Dim h As Long
    Dim m As Long
    Dim s As Long
   
    If lngStart = 0 Then
        lngStart = GetTickCount()  'first time
    End If
   
    'milliseconds since start
    ms = GetTickCount() - lngStart
   
    'break milliseconds into parts
    s = Int(ms / 1000)
    ms = ms - (s * 1000)
   
    m = Int(s / 60)
    s = s - (m * 60)
   
    h = Int(m / 60)
    m = m - (h * 60)
   
    'show current elapsed time
    Label1.Caption = Format(h) & ":" & Format(m, "00") & ":" & Format(s, "00") & "." & Format(ms, "000")
End Sub