4 Replies - 1448 Views - Last Post: 27 September 2010 - 09:33 AM Rate Topic: -----

#1 xzenon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-September 10

Alarm timer with 4 time events

Posted 25 September 2010 - 07:32 AM

Hi
Im new to programming. I hope im not asking the wrong question here. But i got a problem i made an alarm that goes of in just one time but i want it to go of 4 diffrent times insted and i dont realy now how do do it. I would appreciate if any of you could give me a tips or somthing on how i should do.

any way heres my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;




namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        tidpunkt tp = new tidpunkt();
        DateTime alarmTime;


        public Form1()
        {
            InitializeComponent();

            DateTime dt = DateTime.Now;
            tp.Sätt(dt.Hour, dt.Minute, dt.Second);
            a.Text = tp.ToString();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tp.Tick();
            a.Text = tp.ToString();


        }



        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            alarmTime = new DateTime(2010, 9, 25, 10, 36, 00, 00);  
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            alarmTimer.Enabled = true;
        }


        private void alarmTimer_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.AddSeconds(-2) < alarmTime && DateTime.Now.AddSeconds(2) > alarmTime)  
            {
                alarmTimer.Enabled = false;
                MessageBox.Show("Alarm!");

            }



        }
    }

}


This post has been edited by xzenon: 25 September 2010 - 09:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Alarm timer with 4 time events

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Alarm timer with 4 time events

Posted 25 September 2010 - 08:15 AM

Here is the way I would handle it...

1. Store the times of the alarm in a collection(List<DateTime>).
2. Sort the collection.
3. Each time the Tick event happens, check the first element in the collection to see if the current time (DateTime.Now) is greater than the time of that element. If it is, sound the alarm, and remove that time from the collection.
Was This Post Helpful? 0
  • +
  • -

#3 xzenon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-September 10

Re: Alarm timer with 4 time events

Posted 25 September 2010 - 09:14 AM

View Posteclipsed4utoo, on 25 September 2010 - 07:15 AM, said:

Here is the way I would handle it...

1. Store the times of the alarm in a collection(List<DateTime>).
2. Sort the collection.
3. Each time the Tick event happens, check the first element in the collection to see if the current time (DateTime.Now) is greater than the time of that element. If it is, sound the alarm, and remove that time from the collection.


Thx is it possible to modify my code so i can us hour, minute and seconds instead of all the date and time thing i wrote?

This post has been edited by xzenon: 25 September 2010 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: Alarm timer with 4 time events

Posted 25 September 2010 - 02:10 PM

You should be able to use the TimeOfDay property for that. It's a TimeSpan object, so you should be able to check it the current date's TimeOfDay is greater than the specified date's TimeOfDay.
Was This Post Helpful? 1
  • +
  • -

#5 xzenon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-September 10

Re: Alarm timer with 4 time events

Posted 27 September 2010 - 09:33 AM

well after a couple of days i ended up writing this code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;



namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        tidpunkt tp = new tidpunkt();


        public Form1()
        {
            InitializeComponent();
            
            DateTime dt = DateTime.Now;
            tp.Sätt(dt.Hour, dt.Minute, dt.Second);
            a.Text = tp.ToString();
            _soundplayer = new SoundPlayer("redalert.wav");
         }

        private SoundPlayer _soundplayer;
    
         
        


        private TimeSpan _LastAlarm;

        private List<TimeSpan> _Alarms = new List<TimeSpan>()
{
  new TimeSpan(09, 0, 0), 
  new TimeSpan(11, 0, 0), 
  new TimeSpan(12, 0, 0), 
  new TimeSpan(14, 0, 0), 
  new TimeSpan(16, 0, 0) 
};
         
        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tp.Tick();
            a.Text = tp.ToString();

            TimeSpan now = DateTime.Now.TimeOfDay;
            TimeSpan current = new TimeSpan(now.Hours, now.Minutes, 0);// only compare hours and minutes 
            if ((_LastAlarm != current) && (_Alarms.Contains(current)))
            {
                _LastAlarm = current;
                _soundplayer.Play();
                MessageBox.Show("Alarm!!!" + DateTime.Now);
                
         }
        }
      

       


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

    }
}





This post has been edited by xzenon: 27 September 2010 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1