This one slipped by me, I didn't even see it until just now (feel free to PM me to get your tutorials approved faster, if you care).
Excellent tutorial. As all of your tutorials, easily laid out and easy to follow. +rep
Thread: How to create a simple egg timer |
I'm going to show how to create a simple egg timer in VB.Net.
First create a new application and rename the form to frmMain.
Add 3 NumbericUpDowns and change this:
First:
Name = "nudHours"
Maximum = "23"
Second:
Name = "nudMinutes"
Maximum = "59"
Third:
Name = "nudSeconds"
Maximum = "59"
Then add 2 buttons:
First:
Name = "btnStart"
Text = "Start"
Second:
Name = "btnReset"
Text = "Reset"
Then we need the timer to keep track of the time:
Name = "tmrTick"
Interval = "1000"
Now when the items are out we want to start with the code
First we creates a handle for tmrTick.tick:
Code:Private Sub tmrTick_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTick.Tick If nudSeconds.Value <> 0 Then nudSeconds.Value -= 1 Else If nudMinutes.Value <> 0 Then nudMinutes.Value -= 1 nudSeconds.Value = 59 Else If nudHours.Value <> 0 Then nudHours.Value -= 1 nudMinutes.Value = 59 nudSeconds.Value = 59 Else tmrTick.Enabled = False btnStart.Text = "Start" 'What you want should happend when the time is up End If End If End If End Sub
What it does is to decrease the seconds if that is possible else it tries to decrease the minutes and the the hours, if all this is 0 it know the time has ended and it resets the timer and the start button.
Examples on what you can add on the commented line:
Just as single beep:
Shutdown the computer:Code:Beep()
Starting a program:Code:Process.Start("shutdown", "/s")
Playing music:Code:Process.Start("The Path")
orCode:My.Computer.Audio.Play("The Path")
With the second alternative you need to declare this:Code:mciSendString("close audio", CStr(0), 0, 0) mciSendString("Open " & Chr(34) & "The Path" & Chr(34) & " alias audio", CStr(0), 0, 0) mciSendString("play audio", CStr(0), 0, 0)
The second alternative can play more then just .wav files.Code:Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Now when we have the timer done we need to create handlers for the click event of the two buttons, first the btnStart:
This will make the timer start and stop.Code:Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click tmrTick.Enabled = Not tmrTick.Enabled If btnStart.Text = "Start" Then btnStart.Text = "Pause" Else btnStart.Text = "Start" End If End Sub
And then to the btnReset so we can reset all info:
If your program plays a sound when it finish remember to make it stop in this sub.Code:Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click nudHours.Value = 0 nudMinutes.Value = 0 nudSeconds.Value = 0 tmrTick.Enabled = False End Sub
That's all, take care![]()
This one slipped by me, I didn't even see it until just now (feel free to PM me to get your tutorials approved faster, if you care).
Excellent tutorial. As all of your tutorials, easily laid out and easy to follow. +rep
Thanks Jordan.I always make some keywords bold so it should be easier to follow. Is it easier to follow?
Yes. The bold allows scanners to quickly skip and find information. I've read that 90% of people on the Internet only ever scan, rarely reading anything fully.
Hmm, I always sucked at scanning. I read extensively but not a lot. Anyway, Vswe, I like your tutorial but I would prefer seeing DateTime or TimeSpan class with parsing, instead of a bunch of NumericUpDowns. +rep![]()
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
Lol, wrong. Wrong. -rep.(not real rep)
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
There are currently 1 users browsing this thread. (0 members and 1 guests)