SHOW:
|
|
- or go back to the newest paste.
| 1 | Public Class Form1 | |
| 2 | Dim StartTime As DateTime 'old current time | |
| 3 | Dim PauseTime As DateTime 'keep track of the time when the timer is paused | |
| 4 | Dim TotalTimePaused As TimeSpan 'The total amount of time paused | |
| 5 | ||
| 6 | Dim StartTime2 As DateTime 'old current time | |
| 7 | Dim PauseTime2 As DateTime 'keep track of the time when the timer is paused | |
| 8 | Dim TotalTimePaused2 As TimeSpan 'The total amount of time paused | |
| 9 | ||
| 10 | Private Sub BtnStart1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart1.Click | |
| 11 | BtnStart1.Enabled = False | |
| 12 | BtnPause1.Enabled = True | |
| 13 | StartTime = DateTime.Now() | |
| 14 | Timer1.Start() | |
| 15 | End Sub | |
| 16 | Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick | |
| 17 | 'Subtract the current "Now" time from the start time | |
| 18 | 'and then subtract the total time paused | |
| 19 | Dim ElapsedTime As TimeSpan = DateAndTime.Now.Subtract(StartTime).Subtract(TotalTimePaused) | |
| 20 | Label2.Text = ElapsedTime.Hours.ToString.PadLeft(2, "0"c) + ":" + ElapsedTime.Minutes.ToString.PadLeft(2, "0"c) + ":" + ElapsedTime.Seconds.ToString.PadLeft(2, "0"c) | |
| 21 | 'And if you wanted the total elapsed time from time of start then | |
| 22 | 'subtract the current "Now" time from the start time only. | |
| 23 | End Sub | |
| 24 | Private Sub BtnPause1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPause1.Click | |
| 25 | If Timer1.Enabled = False Then | |
| 26 | BtnPause1.Text = "Pause" | |
| 27 | 'Add to the total time paused; the current time minus the | |
| 28 | 'time of pause. this will be the total amount of time | |
| 29 | 'paused so you can subtract it from the total amount of | |
| 30 | 'time since the start button was pressed | |
| 31 | TotalTimePaused = TotalTimePaused.Add(DateAndTime.Now.Subtract(PauseTime)) | |
| 32 | Timer1.Enabled = True | |
| 33 | Else | |
| 34 | BtnPause1.Text = "Resume" | |
| 35 | Timer1.Enabled = False | |
| 36 | 'Set the pause time so it can be | |
| 37 | 'calculated to the total time paused. | |
| 38 | 'when the timer is resumed | |
| 39 | PauseTime = DateAndTime.Now | |
| 40 | End If | |
| 41 | End Sub | |
| 42 | ||
| 43 | Private Sub BtnStop1_Click(sender As Object, e As EventArgs) Handles BtnStop1.Click | |
| 44 | Timer1.Stop() | |
| 45 | BtnStart1.Enabled = True | |
| 46 | End Sub | |
| 47 | ||
| 48 | 'Private Sub BtnStart2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart2.Click | |
| 49 | ' BtnStart2.Enabled = False | |
| 50 | ' BtnPause2.Enabled = True | |
| 51 | ' StartTime2 = DateTime.Now() | |
| 52 | ' Timer2.Start() | |
| 53 | 'End Sub | |
| 54 | 'Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick | |
| 55 | ' 'Subtract the current "Now" time from the start time | |
| 56 | ' 'and then subtract the total time paused | |
| 57 | ' Dim ElapsedTime2 As TimeSpan = DateAndTime.Now.Subtract(StartTime2).Subtract(TotalTimePaused2) | |
| 58 | ' Label4.Text = ElapsedTime2.Hours.ToString.PadLeft(2, "0"c) + ":" + ElapsedTime2.Minutes.ToString.PadLeft(2, "0"c) + ":" + ElapsedTime2.Seconds.ToString.PadLeft(2, "0"c) | |
| 59 | ' 'And if you wanted the total elapsed time from time of start then | |
| 60 | ' 'subtract the current "Now" time from the start time only. | |
| 61 | 'End Sub | |
| 62 | 'Private Sub BtnPause2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPause2.Click | |
| 63 | ' If Timer1.Enabled = False Then | |
| 64 | ' BtnPause2.Text = "Pause" | |
| 65 | ' 'Add to the total time paused; the current time minus the | |
| 66 | ' 'time of pause. this will be the total amount of time | |
| 67 | ' 'paused so you can subtract it from the total amount of | |
| 68 | ' 'time since the start button was pressed | |
| 69 | ' TotalTimePaused2 = TotalTimePaused2.Add(DateAndTime.Now.Subtract(PauseTime2)) | |
| 70 | ' Timer2.Enabled = True | |
| 71 | ' Else | |
| 72 | ' BtnPause2.Text = "Resume" | |
| 73 | ' Timer2.Enabled = False | |
| 74 | ' 'Set the pause time so it can be | |
| 75 | ' 'calculated to the total time paused. | |
| 76 | ' 'when the timer is resumed | |
| 77 | ' PauseTime2 = DateAndTime.Now | |
| 78 | ' End If | |
| 79 | 'End Sub | |
| 80 | ||
| 81 | 'Private Sub BtnStop2_Click(sender As Object, e As EventArgs) Handles BtnStop2.Click | |
| 82 | ' If Timer2.Enabled = False Then | |
| 83 | ' BtnStart2.Enabled = True | |
| 84 | ' Else | |
| 85 | ' BtnStop1.Enabled = False | |
| 86 | ' End If | |
| 87 | 'End Sub | |
| 88 | End Class |