Alekos

I've been searching all over the place and I cant find simple coding to create a simple alarm.

All I want it to do is count down from a given time (given from 3 NumericUpDown controls or Hours, Minutes, Seconds). The alarm than counts down from the given time, and when it hits 0 it plays a sound. Can someone post code on how to do that

I'll make it easy, here is the button click events I'm using as well :

private void btnSetAlarm_Click(object sender, EventArgs e)

{

//In here I want it to prep the timer, setting the time its going to count down and all that.

}

private void btnStartAlarm_Click(object sender, EventArgs e)

{

//In here I need it to actually start the countdown.

}

private void alarmTimer_Tick(object sender, EventArgs e)

{

//Here is probably where it will determine whether or not its finished. Once the timer is counted down I want it to play a sound.

}

Please fill in the blanks. I already set the interval to 1000 on my alarmTimer, so tell me if I need to change that too.

Thanks!

PS: I already looked all over google and I couldn't find anything that really helped. So if you'll please just fill in those spaces, I would be happy. Thanks again!



Re: Visual C# General How to Create an Alarm

Friendly Dog

DateTime _alarmTime;

private void btnSetAlarm_Click(object sender, EventArgs e)

{

_alarmTime = new DateTime(2007, 2, 28, 12, 34, 00, 00); //alarm goes off 2007/2/25 12:34:00

}

private void btnStartAlarm_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) //this is a lossy comparision in case the timer missed the exact second. +/- 2 second might be too much but it's better than alarm not going off. I didn't give more time on this but I think you can figure out a more accurate control

{

alarmTimer.Enabled = false;

// alarm goes off here!!!

}

}






Re: Visual C# General How to Create an Alarm

Alekos

Cool! Thanks!

I think I can work with this!

So to get it to play a sound I would have to add:

private System.Media.SoundPlayer player = new System.Media.SoundPlayer();

as a global, than set it sometime during the load like so:

player.SoundLocation(alarmSound);

Than when it does the huge if statement you posted it would have the alarmTimer.Enabled = false;

Than say: player.PlayLoop();

Right





Re: Visual C# General How to Create an Alarm

Friendly Dog

if you wanted to repeatly play the sound, make sure to add a button for user to stop it !