Jump to content

How to create a simple egg timer

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
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 :D
First we creates a handle for tmrTick.tick:

    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"

                    [B][COLOR="Green"]'What you want should happend when the time is up[/COLOR][/B]

                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:
Beep()

Shutdown the computer:
Process.Start("shutdown", "/s")

Starting a program:
Process.Start("[I]The Path[/I]")

Playing music:
My.Computer.Audio.Play("[I]The Path[/I]")

or

mciSendString("close audio", CStr(0), 0, 0)

mciSendString("Open " & Chr(34) & "[I]The Path[/I]" & Chr(34) & " alias audio", CStr(0), 0, 0)

mciSendString("play audio", CStr(0), 0, 0)

With the second alternative you need to declare this:
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

The second alternative can play more then just .wav files.






Now when we have the timer done we need to create handlers for the click event of the two buttons, first the btnStart:


    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

This will make the timer start and stop.



And then to the btnReset so we can reset all info:

    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

If your program plays a sound when it finish remember to make it stop in this sub.


That's all, take care :D

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Thanks Jordan. :D I always make some keywords bold so it should be easier to follow. Is it easier to follow?

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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.

#5
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
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 ;)

#6
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts

ArekBulski said:

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 ;)

You have a much more control to change the time as user with this. And if you use Dates and TimeSpans you can't see how long time it's left.

#7
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
Lol, wrong. Wrong. -rep. :P (not real rep)

#8
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts

ArekBulski said:

Lol, wrong. Wrong. -rep. :P (not real rep)

Of course you can see the time left but not in the same easy way. And it's like a real egg timer, you can add more time even if it's on, just by changing values when it still counts down.

#9
Demon

Demon

    Newbie

  • Members
  • PipPip
  • 18 posts
The user doesn't have to pick shut down or play sound they could say use a radio button and if checked do this and if the second one's checked do that.