I wrote this tutorial to help you understand the basic of timers.
A Simple ClockStart a new Windows Application project
Add a new Label
Name it
Lbl_Clock Set AutoSize =False
Set Text ="00:00:00"
Add A Timer
Name it
Timer_ClockDouble Click on the Timer
This opens the subroutine that is called every time the interval period is reachedWrite
vb
Private Sub Timer_Clock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_Clock.Tick
' Update clock label Format (HH:MM:SS)
Me.Lbl_Clock.Text = Now.ToLongTimeString
End Sub
Now to set the timer intervals, double click on Form1
vb
'Set Timer_Clock Interval to 1 second (1 Second => 1000 ms)
Me.Timer_Clock.Interval = 1 * 1000
Me.Timer_Clock.Enabled = True
These can be set via the Timer_Clock properties windows, but this demostrates how to do it in code.Run. Now you have a simple clock.
Stop the application, when you are ready for the next part.
A 3-Minute Egg TimerEven though maximum value for interval in 214743674ms (~ 59 Hours)
Let assume in this example it's 100000ms (1 Minute 40 Seconds)
We want the interval to be 180000ms (3 Minutes) how do we do it?
We could count the number of minutes (60000ms) passed.
Add to Form1 a button
Name it
StartEggTimerButton Add a timer, name it
EggTimer Double click on the button and add
vb
' Keep track on how many minutes have passed
Dim MinuteCount As Integer = 0
Private Sub StartEggTimerButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartEggTimerButton.Click
' Set count of minutes to Zero
MinuteCount = 0
' Set Interval to a minute
Me.EggTimer.Interval = 60 * 1000 ' (A minute)
' Start the Egg Timer
Me.EggTimer.Enabled = True
End Sub
Double click on
EggTimer and add
vb
Private Sub EggTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EggTimer.Tick
' Add another minute to the Count of minutes
MinuteCount += 1
' Have I counted 3 minutes?
If MinuteCount = 3 Then
' Yes
' Stop the timer
Me.EggTimer.Enabled = False
' Display a message
MessageBox.Show(Me, "Egg Done")
End If
End Sub
Run the Application
Click the button
I hope this helps you to understand timers.
Now.Eat the nice boiled egg.