Click here to Skip to main content
15,882,114 members
Articles / Programming Languages / C#
Article

Simple PC Alarm Clock

Rate me:
Please Sign up or sign in to vote.
3.29/5 (5 votes)
4 Aug 2007CPOL 65.8K   2.7K   23   3
A simple PC alarm clock in C# using Visual Basic library to Beep
Screenshot - pcalarmclock.jpg

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!

C#
// 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:

VB.NET
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Malaysia Malaysia
Coder and Instructor at:

https://crackinglessons.com
https://crackinglesson.com
https://www.udemy.com/user/paulchin/

Comments and Discussions

 
GeneralThanx Pin
Nitin S4-Feb-09 3:03
professionalNitin S4-Feb-09 3:03 
GeneralBeep... Pin
Landarzar5-Aug-07 0:14
Landarzar5-Aug-07 0:14 
GeneralFew suggestions Pin
J Sullivan4-Aug-07 21:30
J Sullivan4-Aug-07 21:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.