Stopwatch: resume count in new instance

frankinbahia

New Member
Joined
Sep 30, 2013
Messages
9
I’m coding a simple stopwatch, in a UserForm, as shown below. It runs as desired . . . up to a point.
When the user presses Button2 (Start), the timer starts. Press Button3 (Stop) and it stops (or more to the point, it pauses). Press Button2 again and the timer resumes counting. So far, so good.

Now Press Button3 (Stop) again – let’s say the timer is now at 00:00:10 -- and exit. Re-run the code and when it opens, the time display is where you left off before, at 00:00:10. But now comes my problem: When you press Button2 (Start), the timer resets to zero instead of resuming from 10 seconds. Can someone give me a solution (preferably the code itself – I’m a novice). I’m using Excel 2013. Many thanks!
Frank
Bahia, Brazil

Code:
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
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
No ideas out there?
I've also tried filling My.Settings.Proj1TimeSave with something other that Label1.Text, e.g. the timer itself, but nothing works yet.

Thanks again!
 
Upvote 0
I am a complete novice but I am having the same problem and would appreciate any help that anyone might have. My code is different - I have set it out below.
Thanks in advance!

Dim StopTimer As Boolean
Dim Etime As Single
Dim Etime0 As Single
Dim LastEtime As Single

Private Sub ElapsedTimeLbl_Click()

End Sub

Private Sub ExitBtn_Click()
Unload Me
End Sub

Private Sub ResetBtn_Click()
StopTimer = True
Etime = 0
Etime0 = 0
LastEtime = 0
ElapsedTimeLbl.Caption = "00:00:00.00"
[ActiveCell].Value = "00:00:00.00"
End Sub

Private Sub StartBtn_Click()
StopTimer = False
Etime0 = Timer() - LastEtime
Do Until StopTimer
Etime = Int((Timer() - Etime0) * 100) / 100
If Etime > LastEtime Then
LastEtime = Etime
ElapsedTimeLbl.Caption = Format(Etime / 86400, "hh:mm:ss.") & Format(Etime
* 100 Mod 100, "00")
[ActiveCell].Value = Format(Etime / 86400, "hh:mm:ss.") & Format(Etime *
100 Mod 100, "00")
DoEvents
End If
Loop

End Sub

Private Sub StopBtn_Click()
StopTimer = True
Beep
End Sub

Private Sub UserForm_Click()

End Sub
 
Upvote 0

Forum statistics

Threads
1,225,551
Messages
6,185,602
Members
453,307
Latest member
addydata

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top