School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,996 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,973 people online right now. Registration is fast and FREE... Join Now!




Create a simple Stopwatch

 
Reply to this topicStart new topic

> Create a simple Stopwatch, Hours, Minutes, Seconds, 2 buttons

firebolt
Group Icon



post 25 Mar, 2009 - 11:14 PM
Post #1


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

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

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

Private Sub Form_Load()
tmrTime.Enabled = False
End Sub


Finally we code the Exit button. This just exits the program.
CODE

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 post has been edited by firebolt: 26 Mar, 2009 - 11:29 PM


Attached thumbnail(s)
Attached Image
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Dalidar91
*



post 15 Oct, 2009 - 09:24 AM
Post #2
QUOTE(firebolt @ 25 Mar, 2009 - 11:14 PM) *

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

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

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

Private Sub Form_Load()
tmrTime.Enabled = False
End Sub


Finally we code the Exit button. This just exits the program.
CODE

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?
Go to the top of the page
+Quote Post

firebolt
Group Icon



post 15 Oct, 2009 - 04:17 PM
Post #3
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.
Go to the top of the page
+Quote Post

Super_simple
*



post 27 Oct, 2009 - 06:49 PM
Post #4
When I ran the program it had a compile error : Variable not defined the part was: tmrTimer
CODE
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
CODE
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

Go to the top of the page
+Quote Post

NoBrain
Group Icon



post 29 Oct, 2009 - 02:42 PM
Post #5
tmrTime is probably a timer object selected by your components bar. Select it paste it in your form and name it as folow smile.gif then every thing must be ok smile.gif
Go to the top of the page
+Quote Post

rebego
*



post 1 Nov, 2009 - 08:17 PM
Post #6
QUOTE(Dalidar91 @ 15 Oct, 2009 - 09:24 AM) *

QUOTE(firebolt @ 25 Mar, 2009 - 11:14 PM) *

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

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

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

Private Sub Form_Load()
tmrTime.Enabled = False
End Sub


Finally we code the Exit button. This just exits the program.
CODE

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
Go to the top of the page
+Quote Post

firebolt
Group Icon



post 4 Nov, 2009 - 03:06 AM
Post #7
Hey mate,

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

Thanks.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 06:04AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month