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