Need help in debugging a stopwatch program

G

Guest

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

Peter Proost

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
 
C

Cor Ligthert

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
 
G

Guest

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
 
G

Guest

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
 
C

Cor Ligthert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top