SoFunction
Updated on 2025-03-08

C# AlarmClock instance code to implement alarm clock

This article describes a small and simple C# alarm clock program code AlarmClock. The program involves two controls, which may be of some reference value for writing time alarm clocks.

The complete example code is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
namespace AlarmClock
{
 /// <summary>
 /// Summary description of Form1. /// </summary>
 public class Form1 : 
 {
 private  monthCalendar1;
 private  label1;
 private  textBox1;
 private  listBox1;
 private  mask1;
 private  timer1;
 private  buttonAdd;
 private  buttonDel;
 private  components;
 public Form1()
 {
  InitializeComponent();
 }
 protected override void Dispose( bool disposing )
 {
  if( disposing )
  {
  if (components != null) 
  {
   ();
  }
  }
  ( disposing );
 }
 #region Code generated by Windows Form Designer /// <summary>
 /// Designer supports required methods - do not use code editor to modify /// Contents of this method. /// </summary>
 private void InitializeComponent()
 {
   = new ();
   resources = new (typeof(Form1));
  this.monthCalendar1 = new ();
  this.label1 = new ();
  this.mask1 = new ();
  this.textBox1 = new ();
  this.listBox1 = new ();
   = new ();
   = new ();
  this.timer1 = new ();
  (()(this.mask1)).BeginInit();
  ();
  // 
  // monthCalendar1
  // 
  this. = new (12, 112);
  this. = "monthCalendar1";
  this. = 0;
  // 
  // label1
  // 
  this. = new ("Songyi", 14.25F, , , (()(134)));
  this. = new (8, 16);
  this. = "label1";
  this. = new (88, 23);
  this. = 1;
  this. = "label1";
  // 
  // mask1
  // 
  this. = new (16, 48);
  this. = "mask1";
  this. = (()(("")));
  this. = new (88, 23);
  this. = 2;
  // 
  // textBox1
  // 
  this. = new (16, 80);
  this. = "textBox1";
  this. = new (88, 21);
  this. = 3;
  this. = "Input Content";
  // 
  // listBox1
  // 
  this. = 12;
  this. = new (120, 16);
  this. = "listBox1";
  this. = new (152, 52);
  this. = 4;
  // 
  // buttonAdd
  // 
   = new (120, 80);
   = "buttonAdd";
   = new (56, 24);
   = 5;
   = "Add to";
   += new (this.buttonAdd_Click);
  // 
  // buttonDel
  // 
   = new (216, 80);
   = "buttonDel";
   = new (56, 24);
   = 6;
   = "delete";
   += new (this.buttonDel_Click);
  // 
  // timer1
  // 
  this. = true;
  this. = 1000;
  this. += new (this.timer1_Tick);
  // 
  // Form1
  // 
   = new (6, 14);
   = new (292, 266);
  ();
  ();
  (this.listBox1);
  (this.textBox1);
  (this.mask1);
  (this.label1);
  (this.monthCalendar1);
   = "Form1";
   = "Little alarm clock";
   += new (this.Form1_Load);
  (()(this.mask1)).EndInit();
  (false);
 }
 #endregion
 /// <summary>
 /// The main entry point of the application. /// </summary>
 [STAThread]
 static void Main() 
 {
  (new Form1());
 }
 private void timer1_Tick(object sender,  e)
 {
  //Timed Event   = ();
  
  //Timer function  int i;
  //Current time information  int hh = ;
  int mm = ;
  int ss = ;
  for( i = 1; i <= ; i++ )
  {
  string mytime;
  int myhh, mymm, myss;
  //Get the event string  mytime = [i-1].ToString( );
  
  myhh = ( (0,2) );
  if( hh == myhh )
  {
   mymm = ( (2,2) );
   if( mm == mymm )
   {
   myss = ( (4,2) );
   if( ss == myss )
   {
    string tempmsg = (7,  - 7 );
    ( tempmsg, "Alarm clock prompt" );
   }
   }
  }
  }
 }
 private void buttonAdd_Click(object sender,  e)
 {
  String tempstr;
  int tss;
  int tmm;
  int thh;
  //Get the time entered by the user  tempstr = ;
 
  //Judge whether the input is complete  if(  < 6 )
  {
  ( "Input format is wrong, please make up for 0 if there are insufficient places", "Format error" );
  //Set focus  ( );
  //Set the start position to re-enter   = 0;
  //Set the length of the selected character   = 8;
  return;
  }
  //Get the number of hours and make a judgment  thh = ( (0,2) );
  if( thh < 0 || thh > 23 )
  {
  ( "The number of hours should be between 0 and 23", "Error in hours" );
  ( );
   = 0;
   = 2;
  return;
  }
  //Get the number of minutes and make a judgment  tmm = ( (2,2) );
  if( tmm < 0 || tmm > 60 )
  {
  ( "The number of minutes should be between 0 and 60", "Minute Error" );
  ( );
   = 3;
   = 2;
  return;
  }
  //Get the number of seconds and make judgments  tss = ( (4,2) );
  if( tss < 0 || tss > 60 )
  {
  ( "The number of input seconds should be between 0 and 60", "Error in seconds" );
  ( );
   = 6;
   = 2;
  return;
  }
  //Add a timing event to ListBox  tempstr = tempstr + "-" + ;
  ( tempstr );
   = ""; 
 }
 private void buttonDel_Click(object sender,  e)
 {
  //If there is a timed item in the list  if(  > 0 )
  {
  //Delete selected items  if(  > 0 )
  {
   (  );
  }
  }
 }
 private void Form1_Load(object sender,  e)
 {
   = ();
 }
 }
}

The example code in this article has detailed comments, which can help you understand the functions of the code segment well. I hope that the examples described in this article will be of some help to your C# programming design.