Results 1 to 13 of 13

Thread: Simple Alarm clock

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    112

    Simple Alarm clock

    yes hi
    i know there are tons of threads on alarm clocks

    please help on this one

    i have a label control that i want the time to show in

    i just want to display the 24hr version of the time in the label THAT'S it
    Last edited by student_devry01; Jul 26th, 2005 at 08:59 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Alarm clock

    Put a Timer on your form and set Enabled to True and Interval to 1000, then double click it to create a Tick event handler:
    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.         Me.Label1.Text = Date.Now.ToString("H:mm:ss")
    3.     End Sub
    As for the sound file, you should look into the PlaySound API function. It has been mentioned plenty of times on this forum so a search will turn up multiple matches. You could also search for it on MSDN where ther are several examples of how to use it. You would simply check whether the current time is greater than your alarm time and the difference is less than 1 second. Those conditions would indicate that this is the first Tick event after the alarm time and it's time to play the file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    112

    Re: Simple Alarm clock

    the cpu light keeps active
    how do i keep my clock program from using the proessor all the time

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    112

    Re: Simple Alarm clock

    also how do i make the time move around the form like a screensaver

    i have to go to work at 8am
    instead of buying a clock im cheap and wanna build one

    i want the program to run all the time but i don't want a burn in image on my LCD

  5. #5
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Simple Alarm clock

    first add a timer control it is found in toolbox in a design mode of your form
    then do this
    VB Code:
    1. Dim starttime As DateTime
    2.     Dim span, prevspan As TimeSpan
    3.     Dim totalspan As New TimeSpan(0, 0, 0, 0, 0)
    4.  
    5. 'in your form load
    6. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.        Timer1.Start()
    8.     End Sub
    9. 'timer tick event of your timer
    10. Private Sub timertick_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    11.         span = Date.Now.Subtract(starttime)
    12.         totalspan = prevspan
    13.         totalspan = totalspan.Add(span)
    14.         Label1.Text = totalspan.Hours.ToString & " : " & totalspan.Minutes.ToString & ":" & totalspan.Seconds.ToString
    15.     End Sub

    nah jchlimmey got it.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Alarm clock

    Quote Originally Posted by student_devry01
    the cpu light keeps active
    how do i keep my clock program from using the proessor all the time
    A Timer with a 1 second Interval will use very few resources and will not affect your system noticeably. The light on your computer generally indicates hard disk activity rather than processor activity. If your HDD is churning it is not because of the code I provided.

    Burn in on modern monitors is virtually impossible. Screensavers only exist these days because people think they're pretty, not because we need them. If you want to make your Label move around your Form or your Form move around the screen, you should use a second Timer and when it ticks choose some position at random and position your control there:
    VB Code:
    1. Private m_RandNum As New Random(CInt(Date.Now.TimeOfDay.TotalSeconds))
    2.  
    3.     Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    4.         Const minX As Integer = 0
    5.         Const minY As Integer = 0
    6.         Dim maxX As Integer = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
    7.         Dim maxY As Integer = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
    8.  
    9.         Me.Location = New Point(Me.m_RandNum.Next(minX, maxX), Me.m_RandNum.Next(minY, maxY))
    10.     End Sub
    You would set the Timer's Interval to some suitable value like 30000 (30 seconds).
    Last edited by jmcilhinney; Jul 26th, 2005 at 09:52 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    112

    Re: Simple Alarm clock

    how do i link the code together

    the label is call txt_time

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Alarm clock

    You don't unless you want the form to move every second, which could get quite annoying. You put two different Timers on your form. One has an Interval of 1000 and is used to set the Label's Text every second. The second has an Interval of something like 30000 and is used to move the form every 30 seconds. They have completely seperate Tick event handlers. I've changed the name's of the variables to your convention. Just a note though, the "txt" prefix is usually used for a TextBox and "lbl" is used for a Label:
    VB Code:
    1. Private m_RandNum As New Random(CInt(Date.Now.TimeOfDay.TotalSeconds))
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Me.txt_time.Text = Date.Now.ToString("H:mm:ss")
    5.     End Sub
    6.  
    7.     Private Sub tmr_time_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_time.Tick
    8.         Me.txt_time.Text = Date.Now.ToString("H:mm:ss")
    9.     End Sub
    10.  
    11.     Private Sub tmr_location_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_location.Tick
    12.         Const minX As Integer = 0
    13.         Const minY As Integer = 0
    14.         Dim maxX As Integer = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
    15.         Dim maxY As Integer = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
    16.  
    17.         Me.Location = New Point(Me.m_RandNum.Next(minX, maxX), Me.m_RandNum.Next(minY, maxY))
    18.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    112

    Re: Simple Alarm clock

    also i want to remove the exit button when the clock hits midnight

    i am going to setup a different timer for this

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Alarm clock

    You don't need an extra Timer for that. Just test the time in the Tick event handler of the first Timer and if it has just passed midnight then do what you need to.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    112

    Re: Simple Alarm clock

    how??

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    112

    Re: Simple Alarm clock

    i got the clock working
    thanks JMC

    i am trying to prevent someone from viewing my screen between the hours of 12am when i go to sleep and 8am when i wake up

    so the exit button becomes disable at 12 and becomes enable at 8am
    by itself

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Alarm clock

    This code will allow you to perform an action on the first Tick event after midnight. You can do something similar to perform an action at any specified time. Don't place too much code in that Tick event handler, though, because it is being executed once every second. Lot's of code in that method will slow things down. The only way to know when you would notice a difference is to experiment. Also note that some of the variables would be better of being declared at the class level, but I'll leave thos details up to you.
    VB Code:
    1. Private m_LastTick As Date = Date.Now
    2.  
    3.     Private Sub txt_time_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_time.Tick
    4.         Dim currentTime As Date = Date.Now                      'The current time.
    5.         Dim alarmTime As TimeSpan = TimeSpan.FromHours(0)       'The time each day the alarm will go off.  Set as required.
    6.         Dim todaysAlarmTime As Date = Date.Today.Add(alarmTime) 'The time today the alarm should go off.
    7.  
    8.         If Me.m_LastTick < todaysAlarmTime AndAlso currentTime >= todaysAlarmTime Then
    9.             'The alarm time occurred within the time interval since the last tick so initiate the alarm event.
    10.         End If
    11.  
    12.         Me.txt_time.Text = Date.Now.ToString("H:mm:ss")
    13.         Me.m_LastTick = currentTime
    14.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width