• (2 Pages)
  • +
  • 1
  • 2

Create a simple Stopwatch Hours, Minutes, Seconds, 2 buttons Rate Topic: -----

#1 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Posted 26 March 2009 - 12:14 AM

Welcome DIC coders
I will show you how to create a simple stopwatch for visual basic 6 beginners. This stopwatch consists of 3 labels called Label1, Label2, Label3 and 2 buttons called btnSart and btnExit.


1. Create 3 labels named Label1, Label2 and Label3.
2. Change the text of these labels to the number 0.
3. Create a Start/Stop button called btnStart.
4. Create an Exit button called btnExit.
5. Finally, create a timer called tmrTime. Make sure you change the timer interval to 1000 to represent 1 second. This can be placed anywhere in the form as it does not interfere with the design/object.

Then we start our coding. As long as you have labelled everything correctly, you can simple just type all this in without going back and referring to each component.

We will first code the button Start/Stop. This code means if the timer doesnt start automatically, this starts it.
Private Sub btnStart_Click()
If tmrTime.Enabled = False Then
tmrTime.Enabled = True
Else
tmrTime.Enabled = False
End If
End Sub



Next we will code the timer tmrTime. This basically means the timer will gradually add on 1 second and when it reaches 60 secs, the minute label will start. This is the same for the hour label.
Private Sub tmrTime_timer()
Label3.Caption = Val(Label3.Caption) + Val(1)
If Label3.Caption = 60 Then
Label2.Caption = Val(Label2.Caption) + Val(1)
Label3.Caption = 0
ElseIf Label2.Caption = 60 Then
Label1.Caption = Val(Label1.Caption) + Val(1)
Label2.Caption = 0
End If
End Sub



Now we will address the issue of the form. This means when the form loads, the timer will not start yet until you click the start/stop button.
Private Sub Form_Load()
tmrTime.Enabled = False
End Sub



Finally we code the Exit button. This just exits the program.
Private Sub btnExit_Click()
Unload Me
End Sub



Now the form should look similar to the attached image. You can also change the name of the form and give the hours, minutes and second a name label.

Thanks for viewing this tutorial.

Attached image(s)

  • Attached Image

This post has been edited by firebolt: 27 March 2009 - 12:29 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Create a simple Stopwatch

#2 Dalidar91  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 07-October 09

Posted 15 October 2009 - 10:24 AM

View Postfirebolt, on 25 Mar, 2009 - 11:14 PM, said:

Welcome DIC coders
I will show you how to create a simple stopwatch for visual basic 6 beginners. This stopwatch consists of 3 labels called Label1, Label2, Label3 and 2 buttons called btnSart and btnExit.


1. Create 3 labels named Label1, Label2 and Label3.
2. Change the text of these labels to the number 0.
3. Create a Start/Stop button called btnStart.
4. Create an Exit button called btnExit.
5. Finally, create a timer called tmrTime. Make sure you change the timer interval to 1000 to represent 1 second. This can be placed anywhere in the form as it does not interfere with the design/object.

Then we start our coding. As long as you have labelled everything correctly, you can simple just type all this in without going back and referring to each component.

We will first code the button Start/Stop. This code means if the timer doesnt start automatically, this starts it.
Private Sub btnStart_Click()
If tmrTime.Enabled = False Then
tmrTime.Enabled = True
Else
tmrTime.Enabled = False
End If
End Sub



Next we will code the timer tmrTime. This basically means the timer will gradually add on 1 second and when it reaches 60 secs, the minute label will start. This is the same for the hour label.
Private Sub tmrTime_timer()
Label3.Caption = Val(Label3.Caption) + Val(1)
If Label3.Caption = 60 Then
Label2.Caption = Val(Label2.Caption) + Val(1)
Label3.Caption = 0
ElseIf Label2.Caption = 60 Then
Label1.Caption = Val(Label1.Caption) + Val(1)
Label2.Caption = 0
End If
End Sub



Now we will address the issue of the form. This means when the form loads, the timer will not start yet until you click the start/stop button.
Private Sub Form_Load()
tmrTime.Enabled = False
End Sub



Finally we code the Exit button. This just exits the program.
Private Sub btnExit_Click()
Unload Me
End Sub



Now the form should look similar to the attached image. You can also change the name of the form and give the hours, minutes and second a name label.

Thanks for viewing this tutorial.


this code does not work in vb 2008 any suggestions?
Was This Post Helpful? 0
  • +
  • -

#3 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Posted 15 October 2009 - 05:17 PM

Visual Basic 6 is not Visual Basic 2008. You will need a completely different coding. You can always search in the VB.NET tutorials as I'm sure there will be a tutorial on this.
Was This Post Helpful? 0
  • +
  • -

#4 Super_simple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-October 09

Posted 27 October 2009 - 07:49 PM

When I ran the program it had a compile error : Variable not defined the part was: tmrTimer
Private Sub btnStart_Click()

If tmrTimer.Enabled = False Then

tmrTimer.Enabled = True

Else

tmrTimer.Enabled = False

End If

End Sub


but i swear i did everything correct any clues?
the whole code was
Option Explicit
Private Sub tmrTime_Timer()

Label3.Caption = Val(Label3.Caption) + Val(1)

If Label3.Caption = 60 Then

Label2.Caption = Val(Label2.Caption) + Val(1)

Label3.Caption = 0

ElseIf Label2.Caption = 60 Then

Label1.Caption = Val(Label1.Caption) + Val(1)

Label2.Caption = 0

End If

End Sub


Private Sub btnExit_Click()

Unload Me

End Sub

Private Sub btnStart_Click()

If tmrTimer.Enabled = False Then

tmrTimer.Enabled = True

Else

tmrTimer.Enabled = False

End If

End Sub

Private Sub Form_Load()

tmrTime.Enabled = False

End Sub



Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Posted 29 October 2009 - 03:42 PM

tmrTime is probably a timer object selected by your components bar. Select it paste it in your form and name it as folow :) then every thing must be ok :)
Was This Post Helpful? 1
  • +
  • -

#6 rebego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-October 09

Posted 01 November 2009 - 09:17 PM

View PostDalidar91, on 15 Oct, 2009 - 09:24 AM, said:

View Postfirebolt, on 25 Mar, 2009 - 11:14 PM, said:

Welcome DIC coders
I will show you how to create a simple stopwatch for visual basic 6 beginners. This stopwatch consists of 3 labels called Label1, Label2, Label3 and 2 buttons called btnSart and btnExit.


1. Create 3 labels named Label1, Label2 and Label3.
2. Change the text of these labels to the number 0.
3. Create a Start/Stop button called btnStart.
4. Create an Exit button called btnExit.
5. Finally, create a timer called tmrTime. Make sure you change the timer interval to 1000 to represent 1 second. This can be placed anywhere in the form as it does not interfere with the design/object.

Then we start our coding. As long as you have labelled everything correctly, you can simple just type all this in without going back and referring to each component.

We will first code the button Start/Stop. This code means if the timer doesnt start automatically, this starts it.
Private Sub btnStart_Click()
If tmrTime.Enabled = False Then
tmrTime.Enabled = True
Else
tmrTime.Enabled = False
End If
End Sub



Next we will code the timer tmrTime. This basically means the timer will gradually add on 1 second and when it reaches 60 secs, the minute label will start. This is the same for the hour label.
Private Sub tmrTime_timer()
Label3.Caption = Val(Label3.Caption) + Val(1)
If Label3.Caption = 60 Then
Label2.Caption = Val(Label2.Caption) + Val(1)
Label3.Caption = 0
ElseIf Label2.Caption = 60 Then
Label1.Caption = Val(Label1.Caption) + Val(1)
Label2.Caption = 0
End If
End Sub



Now we will address the issue of the form. This means when the form loads, the timer will not start yet until you click the start/stop button.
Private Sub Form_Load()
tmrTime.Enabled = False
End Sub



Finally we code the Exit button. This just exits the program.
Private Sub btnExit_Click()
Unload Me
End Sub



Now the form should look similar to the attached image. You can also change the name of the form and give the hours, minutes and second a name label.

Thanks for viewing this tutorial.


this code does not work in vb 2008 any suggestions?

I changed some of your code to work in VB 2008 and it worked. I was just wondering if you knew how I could write in a code to make the stop watch start at zero when it restarts. Thanks
Was This Post Helpful? 0
  • +
  • -

#7 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Posted 04 November 2009 - 04:06 AM

Hey mate,

Could you please post your question in the VB.NET forum, where there are experts that can help you.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 kaiten65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-December 09

Posted 12 December 2009 - 04:32 PM

View Postrebego, on 1 Nov, 2009 - 08:17 PM, said:

View PostDalidar91, on 15 Oct, 2009 - 09:24 AM, said:

View Postfirebolt, on 25 Mar, 2009 - 11:14 PM, said:

Welcome DIC coders
I will show you how to create a simple stopwatch for visual basic 6 beginners. This stopwatch consists of 3 labels called Label1, Label2, Label3 and 2 buttons called btnSart and btnExit.


1. Create 3 labels named Label1, Label2 and Label3.
2. Change the text of these labels to the number 0.
3. Create a Start/Stop button called btnStart.
4. Create an Exit button called btnExit.
5. Finally, create a timer called tmrTime. Make sure you change the timer interval to 1000 to represent 1 second. This can be placed anywhere in the form as it does not interfere with the design/object.

Then we start our coding. As long as you have labelled everything correctly, you can simple just type all this in without going back and referring to each component.

We will first code the button Start/Stop. This code means if the timer doesnt start automatically, this starts it.
Private Sub btnStart_Click()
If tmrTime.Enabled = False Then
tmrTime.Enabled = True
Else
tmrTime.Enabled = False
End If
End Sub



Next we will code the timer tmrTime. This basically means the timer will gradually add on 1 second and when it reaches 60 secs, the minute label will start. This is the same for the hour label.
Private Sub tmrTime_timer()
Label3.Caption = Val(Label3.Caption) + Val(1)
If Label3.Caption = 60 Then
Label2.Caption = Val(Label2.Caption) + Val(1)
Label3.Caption = 0
ElseIf Label2.Caption = 60 Then
Label1.Caption = Val(Label1.Caption) + Val(1)
Label2.Caption = 0
End If
End Sub



Now we will address the issue of the form. This means when the form loads, the timer will not start yet until you click the start/stop button.
Private Sub Form_Load()
tmrTime.Enabled = False
End Sub



Finally we code the Exit button. This just exits the program.
Private Sub btnExit_Click()
Unload Me
End Sub



Now the form should look similar to the attached image. You can also change the name of the form and give the hours, minutes and second a name label.

Thanks for viewing this tutorial.


this code does not work in vb 2008 any suggestions?

I changed some of your code to work in VB 2008 and it worked. I was just wondering if you knew how I could write in a code to make the stop watch start at zero when it restarts. Thanks

Just Add a Reset button to reset the timer. I am currently testing this code on VB 2010 and it seems to work fine evn though there is the odd places that needs changing. Plus, I am using characters on ONE label text instead of three different boxes. Mail me for the code
Was This Post Helpful? 0
  • +
  • -

#9 3G Alarm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 31-May 09

Posted 11 January 2010 - 01:17 AM

thank you for your tutorial just thinking where i can use this *-*
Was This Post Helpful? 0
  • +
  • -

#10 lil_jheff  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 1
  • Joined: 08-October 10

Posted 08 October 2010 - 08:14 PM

View Postkaiten65, on 12 December 2009 - 03:32 PM, said:

View Postrebego, on 1 Nov, 2009 - 08:17 PM, said:

View PostDalidar91, on 15 Oct, 2009 - 09:24 AM, said:

View Postfirebolt, on 25 Mar, 2009 - 11:14 PM, said:

Welcome DIC coders
I will show you how to create a simple stopwatch for visual basic 6 beginners. This stopwatch consists of 3 labels called Label1, Label2, Label3 and 2 buttons called btnSart and btnExit.


1. Create 3 labels named Label1, Label2 and Label3.
2. Change the text of these labels to the number 0.
3. Create a Start/Stop button called btnStart.
4. Create an Exit button called btnExit.
5. Finally, create a timer called tmrTime. Make sure you change the timer interval to 1000 to represent 1 second. This can be placed anywhere in the form as it does not interfere with the design/object.

Then we start our coding. As long as you have labelled everything correctly, you can simple just type all this in without going back and referring to each component.

We will first code the button Start/Stop. This code means if the timer doesnt start automatically, this starts it.
Private Sub btnStart_Click()
If tmrTime.Enabled = False Then
tmrTime.Enabled = True
Else
tmrTime.Enabled = False
End If
End Sub



Next we will code the timer tmrTime. This basically means the timer will gradually add on 1 second and when it reaches 60 secs, the minute label will start. This is the same for the hour label.
Private Sub tmrTime_timer()
Label3.Caption = Val(Label3.Caption) + Val(1)
If Label3.Caption = 60 Then
Label2.Caption = Val(Label2.Caption) + Val(1)
Label3.Caption = 0
ElseIf Label2.Caption = 60 Then
Label1.Caption = Val(Label1.Caption) + Val(1)
Label2.Caption = 0
End If
End Sub



Now we will address the issue of the form. This means when the form loads, the timer will not start yet until you click the start/stop button.
Private Sub Form_Load()
tmrTime.Enabled = False
End Sub



Finally we code the Exit button. This just exits the program.
Private Sub btnExit_Click()
Unload Me
End Sub



Now the form should look similar to the attached image. You can also change the name of the form and give the hours, minutes and second a name label.

Thanks for viewing this tutorial.


this code does not work in vb 2008 any suggestions?

I changed some of your code to work in VB 2008 and it worked. I was just wondering if you knew how I could write in a code to make the stop watch start at zero when it restarts. Thanks

Just Add a Reset button to reset the timer. I am currently testing this code on VB 2010 and it seems to work fine evn though there is the odd places that needs changing. Plus, I am using characters on ONE label text instead of three different boxes. Mail me for the code

sir i want to know how to use this code..im student and biginner to this program. ..help me. ..
Was This Post Helpful? -1
  • +
  • -

#11 sigfrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-October 10

Posted 09 July 2011 - 03:23 AM

Thank you, this was very helpful..! ^-^
Was This Post Helpful? 0
  • +
  • -

#12 faisalrasid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-July 11

Posted 22 July 2011 - 07:10 AM

Thank you, this is working! :clap:
Was This Post Helpful? 0
  • +
  • -

#13 elgz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-February 13

Posted 19 February 2013 - 09:45 AM

This is 100% works!...
but I don't rename the name of the timer...
Instead using the the default Timer1..
great... and thanks a lot..
Was This Post Helpful? 0
  • +
  • -

#14 woopgfx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 23-February 13

Posted 23 February 2013 - 06:24 AM

View Postfirebolt, on 26 March 2009 - 12:14 AM, said:

Welcome DIC coders
I will show you how to create a simple stopwatch for visual basic 6 beginners. This stopwatch consists of 3 labels called Label1, Label2, Label3 and 2 buttons called btnSart and btnExit.


1. Create 3 labels named Label1, Label2 and Label3.
2. Change the text of these labels to the number 0.
3. Create a Start/Stop button called btnStart.
4. Create an Exit button called btnExit.
5. Finally, create a timer called tmrTime. Make sure you change the timer interval to 1000 to represent 1 second. This can be placed anywhere in the form as it does not interfere with the design/object.

Then we start our coding. As long as you have labelled everything correctly, you can simple just type all this in without going back and referring to each component.

We will first code the button Start/Stop. This code means if the timer doesnt start automatically, this starts it.
Private Sub btnStart_Click()
If tmrTime.Enabled = False Then
tmrTime.Enabled = True
Else
tmrTime.Enabled = False
End If
End Sub



Next we will code the timer tmrTime. This basically means the timer will gradually add on 1 second and when it reaches 60 secs, the minute label will start. This is the same for the hour label.
Private Sub tmrTime_timer()
Label3.Caption = Val(Label3.Caption) + Val(1)
If Label3.Caption = 60 Then
Label2.Caption = Val(Label2.Caption) + Val(1)
Label3.Caption = 0
ElseIf Label2.Caption = 60 Then
Label1.Caption = Val(Label1.Caption) + Val(1)
Label2.Caption = 0
End If
End Sub



Now we will address the issue of the form. This means when the form loads, the timer will not start yet until you click the start/stop button.
Private Sub Form_Load()
tmrTime.Enabled = False
End Sub



Finally we code the Exit button. This just exits the program.
Private Sub btnExit_Click()
Unload Me
End Sub



Now the form should look similar to the attached image. You can also change the name of the form and give the hours, minutes and second a name label.

Thanks for viewing this tutorial.



Hi forgive me if I'm wrong but if you're using anything after VB 2008 you must change the "Label.Caption's" to "Label.Text" and it should work fine! that's what I did anyhow :D
Hope this helps
Cheers
Was This Post Helpful? 1
  • +
  • -

#15 truckn66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-March 10

Posted 17 June 2013 - 06:29 PM

You can replace
Private Sub tmrTime_timer()
...
...
...
...

...


End Sub

With

Private Sub tmrTime_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrTime.Tick
...
...
...
...

...

End Sub
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2