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