By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

Calculate elapsed time

P: n/a
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

Feb 6 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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!
"techspirit" wrote:
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

Feb 6 '06 #2

P: n/a
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

Justin Swan (MSFT) wrote:
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!
"techspirit" wrote:
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


Feb 7 '06 #3

P: n/a
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

Justin Swan (MSFT) wrote:
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!
"techspirit" wrote:
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


Feb 7 '06 #4

P: n/a
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.

Feb 7 '06 #5

P: n/a

techspirit wrote:
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") )

Feb 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.