954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help needed to snooze a C# alarm clock application

The alarm clock application works by setting a certain time and then it perform a certain task eg:sound an alarm, display a messagebox test message. But the problem comes where i need to snooze(delay) the alarm clock for a certain time period like 10 second... Then it will re-initialise the initial alarm clock function.
i tried this but it doesn't work... Why? or is there other methods to snooze the alarm clock?

DateTime SnoozeTime; // global
        private void btnSNOOZE_Click(object sender, EventArgs e)
        {
            pnlDisplay.BackColor = SystemColors.Control;
            myPlayer.Stop();
            SnoozeTime = DateTime.Now.AddSeconds(10);
            Time2.Enabled = true;
        }

        private void Time2_Tick_1(object sender, EventArgs e)
        {
            if (DateTime.Now == SnoozeTime)
            {
                Time.Enabled = false;

                Random Random = new Random();
                Color[] strColor = { Color.Red, Color.Blue, Color.Green, Color.Yellow };
                pnlDisplay.BackColor = strColor[Random.Next(0, strColor.Length)];

                myPlayer.SoundLocation = @"C:\Users\Tan Woon Pang\Desktop\Alarm Clock\Alarm Clock\bin\B.wav";
                myPlayer.PlayLooping();

                MessageBox.Show(txtReminder.Text, "Reminder", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                btnResetTime.Enabled = true;
                btnSNOOZE.Enabled = true;
            }
        }


[/CODE]

imso
Junior Poster in Training
60 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

try using timer.Start() and timer.Stop() rather than changing timer.Enabled.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

I see that the timer event is "Time2_Tick_1" with a 1 at the end. This usually occurs when an event handler is created again for the same object.
Is Time2_Tick_1 used for the original alarm time event?

Note: Using if (DateTime.Now == value) might not trigger as the times are unlikely to ever be exactly the same.
Use if (DateTime.Now >= value) instead.

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

Thanks for above guidance now the problem has party been solved. :)
But may i know why that my class system does not help to check the time value entered beause you can't be entering 99:99 into a 24hour clock time and expect it to run?

private string m_strTime = "";
        private Time m_objTime;

        private void InputTime()
        {
            int intHour;
            int intMinute;

            string strDisplay;

            if (m_strTime.Length > 4)
            {
                m_strTime = m_strTime.Substring(0, 4);
            }

            strDisplay = m_strTime.PadLeft(4, Convert.ToChar("0"));

            intHour = Int32.Parse(strDisplay.Substring(0, 2));
            intMinute = Int32.Parse(strDisplay.Substring(2, 2));

            m_objTime = new Time(intHour, intMinute);
            
            lblAlarmDisplay.Text = String.Format("{0:D2}:{1:D2}", intHour, intMinute);
        }

        private void btnSetTime_Click(object sender, EventArgs e)
        {
            txtReminder.Enabled = false;
            pnlKeyBoard.Enabled = false;
            btnResetTime.Enabled = true;
            Time.Enabled = true;
        }

        private void Time_Tick(object sender, EventArgs e)
        {
            if (string.Format("{0:D2}:{1:D2}", m_objTime.Hour, m_objTime.Minute) == string.Format("{0:HH:mm}", DateTime.Now))
            {
                Time.Enabled = false;

                Random Random = new Random();
                Color[] strColor = { Color.Red, Color.Blue, Color.Green, Color.Yellow };
                pnlDisplay.BackColor = strColor[Random.Next(0, strColor.Length)];

                myPlayer.SoundLocation = @"C:\Users\Tan Woon Pang\Desktop\Alarm Clock\Alarm Clock\bin\B.wav";
                myPlayer.PlayLooping();

                MessageBox.Show(txtReminder.Text, "Reminder", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                btnSNOOZE.Enabled = true;
            }
        }

        DateTime SnoozeTime;

        private void btnSNOOZE_Click(object sender, EventArgs e)
        {
            btnResetTime.Enabled = false;
            pnlDisplay.BackColor = SystemColors.Control;
            myPlayer.Stop();
            SnoozeTime = DateTime.Now.AddSeconds(10);
            TimerCheck.Enabled = true;
        }

        private void TimerCheck_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now >= SnoozeTime)
            {
                TimerCheck.Enabled = false;

                Random Random = new Random();
                Color[] strColor = { Color.Red, Color.Blue, Color.Green, Color.Yellow };
                pnlDisplay.BackColor = strColor[Random.Next(0, strColor.Length)];

                myPlayer.SoundLocation = @"C:\Users\Tan Woon Pang\Desktop\Alarm Clock\Alarm Clock\bin\B.wav";
                myPlayer.PlayLooping();

                MessageBox.Show(txtReminder.Text, "Reminder", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                btnResetTime.Enabled = true;
                btnSNOOZE.Enabled = true;
            }
        }
        
        private void btnResetTime_Click(object sender, EventArgs e)
        {
            btnSNOOZE.Enabled = false;
            pnlKeyBoard.Enabled = true;
            txtReminder.Text = "";
            txtReminder.Enabled = true;
            lblAlarmDisplay.Text = "00:00";
            pnlDisplay.BackColor = SystemColors.Control;
            myPlayer.Stop();
            m_strTime = "";
            m_objTime = new Time(0, 0);
        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Alarm_Clock
{
    public class Time
    {
        private int m_intHour;
        private int m_intMinute;

        public Time(int hourValue, int minuteValue)
        {
            Hour = hourValue;
            Minute = minuteValue;
        }

        public int Hour
        {
            get
            {
                return m_intHour;
            }
            set
            {
                if (value < 24)
                {
                    m_intHour = value;
                }
                else
                {
                    m_intHour = 0;
                }
            }
        }

        public int Minute
        {
            get
            {
                return m_intMinute;
            }
            set
            {
                if (value < 60)
                {
                    m_intMinute = value;
                }
                else
                {
                    m_intMinute = 0;
                }
            }
        }
    }
}
imso
Junior Poster in Training
60 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You