Calculate elapsed time

  • Thread starter Thread starter techspirit
  • Start date Start date
T

techspirit

Hi,

I am seeking help for calculating the elapsed time on an activity.
When the user clicks "pause" button on a form, the timer should also
pause. When the user clicks "continue", the timer should continue from
where the user paused.
For example, if the user paused after 10 seconds of progress in the
activity, it should start from the 11th second when the user clicks
"continue".
Am using .Net version 1.1, so if stopwatch performs a similar action,
it can be ruled out.
Appreciate any help on this using vb.net or directions to a
discussion/link/articles with respect to this query.
Thanks
TS
 
Hi techspirit, I think there's a pretty easy way to accomplish what you're
looking for assuming I understand your situation. I created a small winapp
to show how to do this, it contains 2 buttons, a timer, and a text box. One
Button starts the timer, the other pauses it, and the start button will
resume the timer where it left off. The textbox merely displays the time.
This seems to do what you are looking for. Below I've included the code from
form1.vb:


Public Class Form1

Dim m_seconds As Integer

Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles StartButton.Click
Me.Timer1.Start()
Me.Timer1.Enabled = True
End Sub

Private Sub PauseButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles PauseButton.Click
Me.Timer1.Enabled = False
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
m_seconds = m_seconds + 1
Me.TextBox1.Text = m_seconds.ToString()
End Sub
End Class

Hope this helps!
 
Hi Justin,
Thanks for replying. Your code extract sure works. But, my question is
can I acccomplish the same with a time based structure.I will need to
display the counter in seconds.

Thanks
TS
 
Hi Justin,
Thanks for replying. Your code extract sure works perfect. But, my
question is can I acccomplish the same with a time based structure.I
will need to display the counter in seconds.

Thanks
TS
 
Hi,

Your code was a good place to start working with the counter display.
I used this code , referring to a discussion in this group, to convert
the counter to seconds.
Dim seconds As Integer = 75
Dim elapsed As TimeSpan = TimeSpan.FromSeconds(seconds)
Dim value As DateTime = DateTime.MinValue.Add(elapsed)
Dim s As String = value.ToString("mm:ss")

Thanks, Justin.
 
techspirit said:
Hi,

Your code was a good place to start working with the counter display.
I used this code , referring to a discussion in this group, to convert
the counter to seconds.
Dim seconds As Integer = 75
Dim elapsed As TimeSpan = TimeSpan.FromSeconds(seconds)
Dim value As DateTime = DateTime.MinValue.Add(elapsed)
Dim s As String = value.ToString("mm:ss")

Thanks, Justin.


Ok, I didnt really need all those lines of code. It was much easier to
use :
((New Date).AddSeconds(ElapsedTime).ToString("HH:mm:ss"))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top