Question

In Visual Basic 2010, how do you use the built-in Stopwatch function to less accuracy/with fewer decimal places. I am building a stopwatch. It works fairly well, but has a low framerate (about 5-10 fps) because the stopwatch generates about 10 decimal places! Here's my code to lessen the number of decimal places:

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    rounded = Convert.ToDecimal(stopwatch1.Elapsed)
    rounded2 = Decimal.Round(rounded, 2)
    Label1.Text = Convert.ToString(rounded2)
End Sub

Have a look at this:

Any help?

Thanks!

Était-ce utile?

La solution

I think your best options is just to use a Custom format for the Elapsed value (Which is a TimeSpan) like this:

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    Label1.Text = stopwatch1.Elapsed.ToString("dd\.hh\:mm\:ss\.fff")
End Sub

where fff is the optional number of milliseconds you require - just specify more or less f's as you want.

Note that this won't round the value but in the case of a stopwatch you wouldn't actually want to round it anyway.

Autres conseils

I think your problem is with the timer not the stopwatch. Try setting the interval to a higher value.

Also if you use a DateTimePicker instead of a label you can more easily control the format.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top