|
-
Jul 26th, 2005, 08:50 PM
#1
Thread Starter
Lively Member
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.
-
Jul 26th, 2005, 09:06 PM
#2
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:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Label1.Text = Date.Now.ToString("H:mm:ss")
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.
-
Jul 26th, 2005, 09:12 PM
#3
Thread Starter
Lively Member
Re: Simple Alarm clock
the cpu light keeps active
how do i keep my clock program from using the proessor all the time
-
Jul 26th, 2005, 09:16 PM
#4
Thread Starter
Lively Member
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
-
Jul 26th, 2005, 09:19 PM
#5
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:
Dim starttime As DateTime
Dim span, prevspan As TimeSpan
Dim totalspan As New TimeSpan(0, 0, 0, 0, 0)
'in your form load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
'timer tick event of your timer
Private Sub timertick_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
span = Date.Now.Subtract(starttime)
totalspan = prevspan
totalspan = totalspan.Add(span)
Label1.Text = totalspan.Hours.ToString & " : " & totalspan.Minutes.ToString & ":" & totalspan.Seconds.ToString
End Sub
nah jchlimmey got it.
-
Jul 26th, 2005, 09:49 PM
#6
Re: Simple Alarm clock
 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:
Private m_RandNum As New Random(CInt(Date.Now.TimeOfDay.TotalSeconds))
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Const minX As Integer = 0
Const minY As Integer = 0
Dim maxX As Integer = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
Dim maxY As Integer = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
Me.Location = New Point(Me.m_RandNum.Next(minX, maxX), Me.m_RandNum.Next(minY, maxY))
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.
-
Jul 26th, 2005, 09:56 PM
#7
Thread Starter
Lively Member
Re: Simple Alarm clock
how do i link the code together
the label is call txt_time
-
Jul 26th, 2005, 10:05 PM
#8
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:
Private m_RandNum As New Random(CInt(Date.Now.TimeOfDay.TotalSeconds))
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.txt_time.Text = Date.Now.ToString("H:mm:ss")
End Sub
Private Sub tmr_time_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_time.Tick
Me.txt_time.Text = Date.Now.ToString("H:mm:ss")
End Sub
Private Sub tmr_location_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_location.Tick
Const minX As Integer = 0
Const minY As Integer = 0
Dim maxX As Integer = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
Dim maxY As Integer = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
Me.Location = New Point(Me.m_RandNum.Next(minX, maxX), Me.m_RandNum.Next(minY, maxY))
End Sub
-
Jul 26th, 2005, 10:11 PM
#9
Thread Starter
Lively Member
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
-
Jul 26th, 2005, 10:25 PM
#10
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.
-
Jul 26th, 2005, 10:30 PM
#11
Thread Starter
Lively Member
-
Jul 26th, 2005, 10:34 PM
#12
Thread Starter
Lively Member
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
-
Jul 26th, 2005, 10:56 PM
#13
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:
Private m_LastTick As Date = Date.Now
Private Sub txt_time_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_time.Tick
Dim currentTime As Date = Date.Now 'The current time.
Dim alarmTime As TimeSpan = TimeSpan.FromHours(0) 'The time each day the alarm will go off. Set as required.
Dim todaysAlarmTime As Date = Date.Today.Add(alarmTime) 'The time today the alarm should go off.
If Me.m_LastTick < todaysAlarmTime AndAlso currentTime >= todaysAlarmTime Then
'The alarm time occurred within the time interval since the last tick so initiate the alarm event.
End If
Me.txt_time.Text = Date.Now.ToString("H:mm:ss")
Me.m_LastTick = currentTime
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|