Simple PC Alarm Clock






3.29/5 (5 votes)
A simple PC alarm clock in C# using Visual Basic library to Beep

Introduction
This is a very simple PC alarm clock that can sound the alarm after 1, 2, 3, 4, 5, 10, 15, 20, 30 or 45 minutes. The alarm is a beeping sound at 1 second interval. It is useful if you just want to be reminded of something while using the PC. Maybe you put your kettle on the stove and fear that you may forget. Well, you can use this simple application to remind you.
Background
I know the design could have been better, e.g. using drop down list to enter the duration instead of using buttons, but I was doing this as a challenge to see how fast I can whip up an alarm timer while waiting for my egg on the stove to cook!
// PC Alarm clock by Paul Chin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PC_AlarmClock
{
public partial class Form1 : Form
{
bool timeup=false;
int duration = 0,ticks=0;
public Form1()
{
InitializeComponent();
}
private void button1min_Click(object sender, EventArgs e)
{
duration = 60; //secs
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
duration--;
lblCountdown.Text = duration.ToString();
if (duration == ticks) timeup = true;
//To beep, call the Visual Basic library, remember to add a reference to it
//in the Reference section of Solution Explorer
if(timeup) Microsoft.VisualBasic.Interaction.Beep();
}
private void buttonStop_Click(object sender, EventArgs e)
{
timer1.Stop();
duration = 0;
ticks = 0;
timeup = false;
}
private void button2min_Click(object sender, EventArgs e)
{
duration = 60*2; //secs
timer1.Start();
}
private void button3min_Click(object sender, EventArgs e)
{
duration = 60*3; //secs
timer1.Start();
}
private void button4min_Click(object sender, EventArgs e)
{
duration = 60*4; //secs
timer1.Start();
}
private void button5min_Click(object sender, EventArgs e)
{
duration = 60*5; //secs
timer1.Start();
}
private void button10min_Click(object sender, EventArgs e)
{
duration = 60*10; //secs
timer1.Start();
}
private void button45_Click(object sender, EventArgs e)
{
duration = 60 * 45; //secs
timer1.Start();
}
private void button15min_Click(object sender, EventArgs e)
{
duration = 60 * 15; //secs
timer1.Start();
}
private void button20min_Click(object sender, EventArgs e)
{
duration = 60 * 20; //secs
timer1.Start();
}
private void button30min_Click(object sender, EventArgs e)
{
duration = 60 * 30; //secs
timer1.Start();
}
private void button45min_Click(object sender, EventArgs e)
{
duration = 60 * 45; //secs
timer1.Start();
}
}
}
Points of Interest
I could not find the beep sound function in C#, so I had to include a reference to the Microsoft.VisualBasic library and then call it this way:
Microsoft.VisualBasic.Interaction.Beep();
You will need to add a reference to it by right-clicking Reference (in your Solution Explorer) then select Add Reference.
History
- 4th August, 2007: Initial post