I am using a stop watch to get the elapsed minutes and seconds which will then be displayed to user. How to I go about getting the elapsed time in the following format:
00:00
Printable View
I am using a stop watch to get the elapsed minutes and seconds which will then be displayed to user. How to I go about getting the elapsed time in the following format:
00:00
By stopwatch I assume you mean the timer Object. That is not designed to give you the time between two events. It is designed to trigger after a certain amount of time - say you want a 10 second pause.
What I would do is use the Now() function to get the current time at the start of the process and end of the process. Then used the DateDiff function to determine the time difference between the two times in Seconds. Once you have the number of seconds, you do a little math to figure out how many minutes and seconds, then format that, just building it up. You will have to format you minutes and seconds to ensure they give you two numbers. You can use a Format string for that.
I hope that helps.
Ok cool i'm actually using the stopwatch class not a timer. I can get the time but am not sure about how to format the time.
Hey,
Have you looked at the documentation for the StopWatch Class?
http://msdn.microsoft.com/en-us/libr...stopwatch.aspx
It would appear to tell you how to do exactly what you want.
Gary
Heres is some code for what i used. The format i used was Hour:Minute:Seconds:Millisecond
Ive Changed it for your 00:00Code:Private watch As Diagnostics.Stopwatch
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Timer1.Enabled = True
Me.watch = Diagnostics.Stopwatch.StartNew
Me.Timer1.Start()
End Sub
Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Timer1.Stop()
Timer1.Dispose()
Timer1.Enabled = False
lblTime.Text = ("00:00")
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsed As TimeSpan = Me.watch.Elapsed
Me.lblTime.Text = String.Format("{0:00}:{1:00}", _
elapsed.Seconds, _
elapsed.Milliseconds)
End Sub
Ok thanks guys problem solved