Ds.109
Ds.109

Reputation: 730

Display Value of timer - VB

I can't figure out how to simply display to value of a timer after it's stopped in VB.

I tried stuff like MsgBox(Val(Timer2)) but it's not that simple apparently.

Upvotes: 0

Views: 11353

Answers (3)

Steve
Steve

Reputation: 20459

Perhaps the stopwatch class would work for you:

    Dim sw As New Stopwatch
    sw.Start()
    'do something, for example:
    Threading.Thread.Sleep(1000)
    sw.Stop()
    MessageBox.Show(sw.ElapsedMilliseconds)

Upvotes: 3

prprcupofcoffee
prprcupofcoffee

Reputation: 2970

Timers don't do that. But you can use various date/time functions to do the same thing.

Dim startTime As DateTime
Dim elapsed As DateTime
' ...
Timer2.Start()
startTime = Now

' after race finishes, then:
elapsed = Now - startTime

This gives you a TimeSpan object.

or

Dim startTime As Integer = Environment.TickCount
' ...
Dim elapsed As Integer = Environment.TickCount - startTime

This gives you an overall time in milliseconds.

Upvotes: 1

Kratz
Kratz

Reputation: 4330

I think what you want is this,

Dim StartTime = DateTime.Now()

'Do race stuff



Dim EndTime = DateTime.Now()

Dim ElapsedSeconds = EndTime.Subtract(StartTime).TotalSeconds

Upvotes: 1

Related Questions