Stop Watch Counter (Elapsed Time)

paulpitchford

Member
Joined
Jul 6, 2006
Messages
5
Programming Experience
3-5
Hi,

Thanks to another thread on this site I have found some code to countdown from a set period of time:

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Timer1_Tick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Timer1.Tick[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] alarmTime < [/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2].Now [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]      Me[/COLOR][/SIZE][SIZE=2].Timer1.Stop()
        MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"Time's up."[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]      Dim[/COLOR][/SIZE][SIZE=2] remainingTime [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TimeSpan = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].alarmTime.Subtract([/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2].Now)
[/SIZE][SIZE=2][COLOR=#0000ff]      Me[/COLOR][/SIZE][SIZE=2].Label1.Text = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#800000]"{0}:{1:d2}:{2:d2}"[/COLOR][/SIZE][SIZE=2], _
                                                    remainingTime.Hours, _
                                                    remainingTime.Minutes, _
                                                    remainingTime.Seconds)
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#008000]'Then in the command button code[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2][COLOR=#000000].alarmTime = [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2][COLOR=#000000].Now.AddMinutes(30)[/COLOR]
[/SIZE][/COLOR][/SIZE][/SIZE]

What I am now looking for is some code that starts at 0:00:00 and increments like a stopwatch second by second (0:00:01... 0:00:02 and so on)

Any help would be appreciated. I'm farily new to VB.NET so go easy on me! ;)

Thanks in advance.
Kind Regards,

Paul Pitchford.
 
Add a label control and change the text to "0:0:0". then just add this procedure to the tick event of the timer object.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] myStopWatch()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] parts() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Label1.Text.Split([/SIZE][SIZE=2][COLOR=#800000]":"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cHours [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](parts(0), [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cMins [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](parts(1), [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cSecs [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](parts(2), [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] cSecs >= 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]  cMins = cMins + 1
  cSecs = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]  cSecs = cSecs + 1
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] cMins >= 60 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]  cHours = cHours + 1
  cMins = 0
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] cHours < 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] cHours = [/SIZE][SIZE=2][COLOR=#800000]"0"[/COLOR][/SIZE][SIZE=2] & cHours
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] cMins < 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] cMins = [/SIZE][SIZE=2][COLOR=#800000]"0"[/COLOR][/SIZE][SIZE=2] & cMins
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] cSecs < 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] cSecs = [/SIZE][SIZE=2][COLOR=#800000]"0"[/COLOR][/SIZE][SIZE=2] & cSecs
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label1.Text = cHours & [/SIZE][SIZE=2][COLOR=#800000]":"[/COLOR][/SIZE][SIZE=2] & cMins & [/SIZE][SIZE=2][COLOR=#800000]":"[/COLOR][/SIZE][SIZE=2] & cSecs
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

Regards ;)
 
Hi and thanks for your help so far!

The example you gave seems to count to 10 seconds and then increment the minute by 1?? also I would like the format of 0:00:00 rather than 0:0:0. Is there not a way to use the TimeSpan type similar to the count down example?

Thanks,

Paul.
 
Well i don't know another way(s) ... except this one. If someone knows i am also interested.
Till then let me show you how to fix that to appear in format 00:00:00 instead

just add this If statements there (maybe you should check more combintaions)
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] cHours < 10 [/SIZE][SIZE=2][COLOR=#0000ff]AndAlso[/COLOR][/SIZE][SIZE=2] cMins < 10 [/SIZE][SIZE=2][COLOR=#0000ff]AndAlso[/COLOR][/SIZE][SIZE=2] cSecs < 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Me[/COLOR][/SIZE][SIZE=2].Label1.Text = [/SIZE][SIZE=2][COLOR=#800000]"0"[/COLOR][/SIZE][SIZE=2] & cHours & [/SIZE][SIZE=2][COLOR=#800000]":0"[/COLOR][/SIZE][SIZE=2] & cMins & [/SIZE][SIZE=2][COLOR=#800000]":0"[/COLOR][/SIZE][SIZE=2] & cSecs
[/SIZE][SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][SIZE=2] cSecs >= 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]      Me[/COLOR][/SIZE][SIZE=2].Label1.Text = [/SIZE][SIZE=2][COLOR=#800000]"0"[/COLOR][/SIZE][SIZE=2] & cHours & [/SIZE][SIZE=2][COLOR=#800000]":0"[/COLOR][/SIZE][SIZE=2] & cMins & [/SIZE][SIZE=2][COLOR=#800000]":"[/COLOR][/SIZE][SIZE=2] & cSecs
[/SIZE][SIZE=2][COLOR=#0000ff]   End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] cHours < 10 [/SIZE][SIZE=2][COLOR=#0000ff]AndAlso[/COLOR][/SIZE][SIZE=2] cMins < 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Me[/COLOR][/SIZE][SIZE=2].Label1.Text = [/SIZE][SIZE=2][COLOR=#800000]"0"[/COLOR][/SIZE][SIZE=2] & cHours & [/SIZE][SIZE=2][COLOR=#800000]":0"[/COLOR][/SIZE][SIZE=2] & cMins & [/SIZE][SIZE=2][COLOR=#800000]":"[/COLOR][/SIZE][SIZE=2] & cSecs
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] cHours < 10 [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] cMins >= 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Me[/COLOR][/SIZE][SIZE=2].Label1.Text = [/SIZE][SIZE=2][COLOR=#800000]"0"[/COLOR][/SIZE][SIZE=2] & cHours & [/SIZE][SIZE=2][COLOR=#800000]":"[/COLOR][/SIZE][SIZE=2] & cMins & [/SIZE][SIZE=2][COLOR=#800000]":"[/COLOR][/SIZE][SIZE=2] & cSecs
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Me[/COLOR][/SIZE][SIZE=2].Label1.Text = cHours & [/SIZE][SIZE=2][COLOR=#800000]":"[/COLOR][/SIZE][SIZE=2] & cMins & [/SIZE][SIZE=2][COLOR=#800000]":"[/COLOR][/SIZE][SIZE=2] & cSecs
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
less code and more reliable, the timer interval is prone to lagging in stressed situations:
VB.NET:
Private starttime As Date
 
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
    Dim ts As TimeSpan = Date.Now.Subtract(starttime)
    Label1.Text = String.Format("{0}:{1:d2}:{2:d2}", ts.Hours, ts.Minutes, ts.Seconds)
End Sub
 
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnStart.Click
    starttime = Date.Now
    Timer1.Enabled = True
End Sub
 
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnStop.Click
    Timer1.Enabled = False
End Sub
 
Back
Top