By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

Need help in debugging a stopwatch program

P: n/a
I have written a program that functions similarly to a stopwatch.
When the user clicks the button, a label on the form starts counting. The
program, however, does not tick once a second. How can I fix this?

The following is the program. It assumes that the forms has a Button named
Button1, a Label named Label1 and a Timer named Timer1.
Dim sec, min As Double
Private Sub InitializeTimer()
' Run this procedure in an appropriate event.
' Set to 1 second.
Timer1.Interval = 1000
' Enable timer.
Timer1.Enabled = True
Button1.Text = "Enabled"
End Sub

Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs)
Handles Timer1.Tick
' Set the caption to display the current value of sec
Label1.Text = min & ":" & sec
sec += 1
If sec = 60 Then
min += 1
sec = 0
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Button1.Text = "Stop" Then
Button1.Text = "Start"
Timer1.Enabled = False
Else
Button1.Text = "Stop"
Timer1.Enabled = True
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
sec = 0
min = 0
End Sub

Thanks in advance.
--
Xero

http://www.chezjeff.net
My personal web portal
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
add label1.refresh

Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs)
Handles Timer1.Tick
' Set the caption to display the current value of sec
Label1.Text = min & ":" & sec
sec += 1
If sec = 60 Then
min += 1
sec = 0
End If
label1.refresh
End Sub

hth Peter
"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )> wrote in message
news:8C**********************************@microsof t.com...
I have written a program that functions similarly to a stopwatch.
When the user clicks the button, a label on the form starts counting. The
program, however, does not tick once a second. How can I fix this?

The following is the program. It assumes that the forms has a Button named
Button1, a Label named Label1 and a Timer named Timer1.
Dim sec, min As Double
Private Sub InitializeTimer()
' Run this procedure in an appropriate event.
' Set to 1 second.
Timer1.Interval = 1000
' Enable timer.
Timer1.Enabled = True
Button1.Text = "Enabled"
End Sub

Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs)
Handles Timer1.Tick
' Set the caption to display the current value of sec
Label1.Text = min & ":" & sec
sec += 1
If sec = 60 Then
min += 1
sec = 0
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Button1.Text = "Stop" Then
Button1.Text = "Start"
Timer1.Enabled = False
Else
Button1.Text = "Stop"
Timer1.Enabled = True
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
sec = 0
min = 0
End Sub

Thanks in advance.
--
Xero

http://www.chezjeff.net
My personal web portal

Nov 21 '05 #2

P: n/a
Jeff,

Did I not give you an example for this.

In that answer I said that a "timer" is not to measure time, however is an
alarm clock.

I gave you an example with the tickcount and gave you extra a link to the
timespan class because you have than a probably better showable span time.

I had a short discussion in that thread with Herfried, where he told how
you could use a timer. I asked him if they measure in Austria the time at
sport events (not setting the game time) with an alarm clock, and on that he
did not answer anymore.

Therefore why are you asking the same question again and are you using the
timer (an alarm clock) as a stopwatch?

Cor
"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )>
I have written a program that functions similarly to a stopwatch.
When the user clicks the button, a label on the form starts counting. The
program, however, does not tick once a second. How can I fix this?

The following is the program. It assumes that the forms has a Button named
Button1, a Label named Label1 and a Timer named Timer1.
Dim sec, min As Double
Private Sub InitializeTimer()
' Run this procedure in an appropriate event.
' Set to 1 second.
Timer1.Interval = 1000
' Enable timer.
Timer1.Enabled = True
Button1.Text = "Enabled"
End Sub

Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs)
Handles Timer1.Tick
' Set the caption to display the current value of sec
Label1.Text = min & ":" & sec
sec += 1
If sec = 60 Then
min += 1
sec = 0
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Button1.Text = "Stop" Then
Button1.Text = "Start"
Timer1.Enabled = False
Else
Button1.Text = "Stop"
Timer1.Enabled = True
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
sec = 0
min = 0
End Sub

Thanks in advance.
--
Xero

http://www.chezjeff.net
My personal web portal

Nov 21 '05 #3

P: n/a
Cor,

I have read the discussion between you and Herfried as well.

Herfried said that a Timer raises an event periodically. Therefore I thought
if I set the time interval to 1000 milliseconds (i.e. 1 second) and increase
the value of a label by 1, the program will function just like a stopwatch.

Perhaps I did not make myself clear enough. Actually I want that label
refreshes itself once a second. The code you sent to me seemed to be
displaying the total time elapsed only when the user does something (clicks a
button, type something or whatever) rather than refreshing itself
automatically.

Anyway I have achieved what I want. Thanks for discussing this problem with
me! :)

Jeff
"Cor Ligthert" wrote:
Jeff,

Did I not give you an example for this.

In that answer I said that a "timer" is not to measure time, however is an
alarm clock.

I gave you an example with the tickcount and gave you extra a link to the
timespan class because you have than a probably better showable span time.

I had a short discussion in that thread with Herfried, where he told how
you could use a timer. I asked him if they measure in Austria the time at
sport events (not setting the game time) with an alarm clock, and on that he
did not answer anymore.

Therefore why are you asking the same question again and are you using the
timer (an alarm clock) as a stopwatch?

Cor
"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )>
I have written a program that functions similarly to a stopwatch.
When the user clicks the button, a label on the form starts counting. The
program, however, does not tick once a second. How can I fix this?

The following is the program. It assumes that the forms has a Button named
Button1, a Label named Label1 and a Timer named Timer1.
Dim sec, min As Double
Private Sub InitializeTimer()
' Run this procedure in an appropriate event.
' Set to 1 second.
Timer1.Interval = 1000
' Enable timer.
Timer1.Enabled = True
Button1.Text = "Enabled"
End Sub

Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs)
Handles Timer1.Tick
' Set the caption to display the current value of sec
Label1.Text = min & ":" & sec
sec += 1
If sec = 60 Then
min += 1
sec = 0
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Button1.Text = "Stop" Then
Button1.Text = "Start"
Timer1.Enabled = False
Else
Button1.Text = "Stop"
Timer1.Enabled = True
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
sec = 0
min = 0
End Sub

Thanks in advance.
--
Xero

http://www.chezjeff.net
My personal web portal


Nov 21 '05 #4

P: n/a
Cor,

I have read the discussion between you and Herfried as well.

Herfried said that a Timer raises an event periodically. Therefore I thought
if I set the time interval to 1000 milliseconds (i.e. 1 second) and increase
the value of a label by 1, the program will function just like a stopwatch.

Perhaps I did not make myself clear enough. Actually I want that label
refreshes itself once a second. The code you sent to me seemed to be
displaying the total time elapsed only when the user does something (clicks a
button, type something or whatever) rather than refreshing itself
automatically.

Anyway I have achieved what I want. Thanks for discussing this problem with
me! :)

Jeff
"Cor Ligthert" wrote:
Jeff,

Did I not give you an example for this.

In that answer I said that a "timer" is not to measure time, however is an
alarm clock.

I gave you an example with the tickcount and gave you extra a link to the
timespan class because you have than a probably better showable span time.

I had a short discussion in that thread with Herfried, where he told how
you could use a timer. I asked him if they measure in Austria the time at
sport events (not setting the game time) with an alarm clock, and on that he
did not answer anymore.

Therefore why are you asking the same question again and are you using the
timer (an alarm clock) as a stopwatch?

Cor
"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )>
I have written a program that functions similarly to a stopwatch.
When the user clicks the button, a label on the form starts counting. The
program, however, does not tick once a second. How can I fix this?

The following is the program. It assumes that the forms has a Button named
Button1, a Label named Label1 and a Timer named Timer1.
Dim sec, min As Double
Private Sub InitializeTimer()
' Run this procedure in an appropriate event.
' Set to 1 second.
Timer1.Interval = 1000
' Enable timer.
Timer1.Enabled = True
Button1.Text = "Enabled"
End Sub

Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs)
Handles Timer1.Tick
' Set the caption to display the current value of sec
Label1.Text = min & ":" & sec
sec += 1
If sec = 60 Then
min += 1
sec = 0
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Button1.Text = "Stop" Then
Button1.Text = "Start"
Timer1.Enabled = False
Else
Button1.Text = "Stop"
Timer1.Enabled = True
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
sec = 0
min = 0
End Sub

Thanks in advance.
--
Xero

http://www.chezjeff.net
My personal web portal


Nov 21 '05 #5

P: n/a
Xero,
I have read the discussion between you and Herfried as well.

Herfried said that a Timer raises an event periodically. Therefore I
thought
if I set the time interval to 1000 milliseconds (i.e. 1 second) and
increase
the value of a label by 1, the program will function just like a
stopwatch.

Perhaps I did not make myself clear enough. Actually I want that label
refreshes itself once a second. The code you sent to me seemed to be
displaying the total time elapsed only when the user does something
(clicks a
button, type something or whatever) rather than refreshing itself
automatically.


In my opinion is the difference between a clock and a stopwatch that a
stopwatch starts and stops with a buttonclick.

However have a lot of success with your alarmclock used as stopwatch, I am
glad you took this Herfried solution to measure the time.

I advised you to take the timespan or the environment tickcount. (While
there is an even more precise one however that takes API's.)

When you say that when Herfried find my advise not right and than take
automatic the method from Herfried without even to give a reaction, than I
keep that in mind of course for your next question.

When it was about to show a going clock *meanwhile* there are even samples
to show an analog clock. However that has nothing to do with measure time,
that is just showing.

Have a lot of success with your method advised by Herfried.

Cor
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.