+ Reply to Thread
Results 1 to 8 of 8

Thread: How to create a simple egg timer

  1. #1
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    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. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    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

  3. #3
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    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?

  4. #4
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    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.

  5. #5
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    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

  6. #6
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    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.

  7. #7
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Re: How to create a simple egg timer

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

  8. #8
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    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.

+ 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. create simple key logger with C++
    By vaheb in forum C and C++
    Replies: 5
    Last Post: 08-20-2010, 03:22 PM
  2. A simple timer
    By travy92 in forum VB Tutorials
    Replies: 3
    Last Post: 02-15-2010, 07:28 AM
  3. How to create a Bandwidth Monitor
    By Dren in forum VB Tutorials
    Replies: 44
    Last Post: 08-15-2009, 02:02 AM
  4. making a simple discusion forum part 1 (the database)
    By omar_a_k in forum PHP Tutorials
    Replies: 3
    Last Post: 01-14-2009, 11:24 AM
  5. Simple timer
    By im-not-alive in forum Java Help
    Replies: 1
    Last Post: 01-22-2008, 10:57 AM