+ Reply to Thread
Results 1 to 9 of 9

Thread: How to create a simple egg timer

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    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:
    Code:
    Beep()
    Shutdown the computer:
    Code:
    Process.Start("shutdown", "/s")
    Starting a program:
    Code:
    Process.Start("The Path")
    Playing music:
    Code:
    My.Computer.Audio.Play("The Path")
    or

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


    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
    This will make the timer start and stop.



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

    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
    If your program plays a sound when it finish remember to make it stop in this sub.


    That's all, take care

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: How to create a simple egg timer

    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

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: How to create a simple egg timer

    Thanks Jordan. I always make some keywords bold so it should be easier to follow. Is it easier to follow?

  5. #4
    Jordan Guest

    Re: How to create a simple egg timer

    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.

  6. #5
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Re: How to create a simple egg timer

    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

  7. #6
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: How to create a simple egg timer

    Quote Originally Posted by ArekBulski View Post
    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.

  8. #7
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Re: How to create a simple egg timer

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

  9. #8
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: How to create a simple egg timer

    Quote Originally Posted by ArekBulski View Post
    Lol, wrong. Wrong. -rep. (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.

  10. #9
    Demon is offline Newbie
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    6

    Re: How to create a simple egg timer

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to create a countdown timer in Javascript
    By Vswe in forum JavaScript Tutorials
    Replies: 10
    Last Post: 06-09-2011, 12:58 PM
  2. create handle to a custom timer class?
    By alirezan in forum C and C++
    Replies: 4
    Last Post: 04-29-2011, 07:35 AM
  3. create simple key logger with C++
    By vaheb in forum C and C++
    Replies: 5
    Last Post: 08-20-2010, 04:22 PM
  4. A simple timer
    By travy92 in forum Visual Basic Tutorials
    Replies: 3
    Last Post: 02-15-2010, 07:28 AM
  5. Simple timer
    By im-not-alive in forum Java Help
    Replies: 1
    Last Post: 01-22-2008, 10:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts