Welcome to Dream.In.Code
Getting Help is Easy!

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




Using Timer

 
Reply to this topicStart new topic

> Using Timer, A Simple Clock & Egg Timer

AdamSpeight2008
Group Icon



post 19 Jul, 2008 - 09:52 AM
Post #1


I wrote this tutorial to help you understand the basic of timers.
A Simple Clock
Start 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_Clock

Double Click on the Timer
This opens the subroutine that is called every time the interval period is reached
Write
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 Timer

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

Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

sam.adams61
*



post 24 Jul, 2008 - 04:23 PM
Post #2
Many thanks Adam for your many contributions...as a raw beginner (and not a young one either) I found this tutorial very helpful.
Go to the top of the page
+Quote Post

Brassconstruction
*



post 14 Nov, 2008 - 10:09 PM
Post #3
I am a new guy to this forum and am glad i did.This project from Adam is quite nice.Keep it up.
Go to the top of the page
+Quote Post


Fast ReplyReply 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: 12/1/08 05:26PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month