Resume stopwatch/timer's position in a new instance

Joined
Oct 6, 2013
Messages
7
Programming Experience
Beginner
What I?m trying to do:
Save the elapsed time of a stopwatch (in My.Settings) so that it can be recalled and effectively used as the start time in a new instance.

Where I?m at:
I?ve only got as far as setting up the standard stopwatch, which runs fine. Then when the user presses Button3 (Stop), it saves the current value of the timer?s display in My.Settings.) Let?s say the timer is now at 00:00:10 and the user exits. Re-run the code and when it opens, the time display is where you left off before, at 00:00:10. But now comes the problem: When you press Button2 (Start), the timer resets to zero instead of resuming from 10 seconds.

I suspect that the solution lies in adding the value stored in My.Settings to that of the timer at launch (which I assume is zero, irrespective of what the display shows), but I don?t know how to do that. Your suggestions (preferably in code ? I?m a novice) would be much appreciated.

Here?s the code. It?s written in VB/Visual Studio 2013.

VB.NET:
[LIST=1]
[*]Public Class Form1
[*]    Private stopwatch As New Stopwatch
[*]
[*]    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal         e As System.EventArgs) Handles Timer1.Tick
[*]        Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
[*]               Label1.Text = String.Format("{0:00}:{1:00}:{2:00}", Math.Floor(elapsed.TotalHours), elapsed.Minutes, elapsed.Seconds)
[*]    End Sub
[*]
[*]    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
[*]        Timer1.Start()
[*]        Me.stopwatch.Start()
[*]        Button4.Enabled = False
[*]    End Sub
[*]
[*]    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
[*]        Timer1.Stop()
[*]        Me.stopwatch.Stop()
[*]        Button4.Enabled = True
[*]        My.Settings.Proj1TimeSave = Label1.Text
[*]        My.Settings.Save()
[*]    End Sub
[*]
[*]    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
[*]        Me.stopwatch.Reset()
[*]        Label1.Text = "00:00:00"
[*]    End Sub
[*]
[*]    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
[*]        Dim project1name As String
[*]        project1name = TextBox1.Text
[*]        My.Settings.Proj1NameSave = TextBox1.Text
[*]        My.Settings.Save()
[*]    End Sub
[*]
[*]
[*]    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
[*]        TextBox1.Text = My.Settings.Proj1NameSave
[*]        Label1.Text = My.Settings.Proj1TimeSave
[*]   End Sub
[*]
[*]End Class
[/LIST]
(Note: A similar post was made in another forum last week, but no replies.)
 
You can't start a Stopwatch from anywhere but zero, but that doesn't mean that you can't simply add another TimeSpan to the Elapsed property and then use the result. Simply create a setting of type TimeSpan and then, whenever you want the total elapsed time you do this:
Dim totalElapsedTime = myStopwatch.Elapsed + My.Settings.ElapsedTime
When the app closes you save that total to the setting and then that's your offset when you start again next time.
 
Back
Top