UserControl1.cs
上传用户:sz2001
上传日期:2022-07-12
资源大小:85k
文件大小:10k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace alarm
  9. {
  10.     public partial class alarm : UserControl
  11.     {
  12.         [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
  13.         public class Author : Attribute
  14.         {
  15.             public Author(string name,double versn)
  16.             {
  17.                 myName = name; 
  18.                 version = versn;
  19.             }
  20.             public double version;
  21.             string myName;
  22.             public string GetName()
  23.             {
  24.                 return myName;
  25.             }
  26.         }
  27.         //设置变量
  28.         [Author("欧阳昭暐", 1.0)]
  29.         DateTime tmpTime;//记录用户设置的临时时间变量
  30.         string message;//记录用户设置的提示信息
  31.         DateTime[] arrytime = new DateTime[3];//时间数组记录已设置的多个定时
  32.         string[,] allAla = new string[3,2];//二维数组记录已设置的时间字串和提示信息字符串
  33.         int alrNum = 0;//记录已设置的闹钟个数
  34.         int tmpdiff;//记录当前定时差值临时变量
  35.         int[] timediff = new int[3];//记录已设置闹钟的定时差值
  36.         [Author("欧阳昭暐",1.0)]
  37.         public alarm()
  38.         {
  39.             InitializeComponent();
  40.         }
  41.         //显示当前时间
  42.         private void timer1_Tick(object sender, EventArgs e)
  43.         {
  44.             this.timelab.Text = DateTime.Now.ToLongTimeString();
  45.         }
  46.         //设置闹钟
  47.         [Author("欧阳昭暐", 1.0)]
  48.         void AddTime()
  49.         {
  50.             if (alrNum >= 3)//判断是否超过定时器上限
  51.             {
  52.                 MessageBox.Show("对不起,定时器已满~");
  53.             }
  54.             else
  55.             {
  56.                 if (this.megBox.Text == "")//判断用户是否输入了提示信息
  57.                 {
  58.                     MessageBox.Show("请输入提示信息!");
  59.                     return;
  60.                 }
  61.                 tmpTime = this.dateTimePicker1.Value;//记录定时时间
  62.                 message = this.megBox.Text;//记录定时提示信息
  63.                 tmpdiff = ((int)tmpTime.Hour * 10000 + (int)tmpTime.Minute * 100 + (int)tmpTime.Second) - ((int)DateTime.Now.Hour * 10000 + (int)DateTime.Now.Minute * 100 + (int)DateTime.Now.Second);
  64.                //记录当前时间差值
  65.                 int j = 0;//记录已设置定时的时间差
  66.                 timediff[j] = ((int)arrytime[j].Hour * 10000 + (int)arrytime[j].Minute * 100 + (int)arrytime[j].Second) - ((int)DateTime.Now.Hour * 10000 + (int)DateTime.Now.Minute * 100 + (int)DateTime.Now.Second);
  67.                 for (int i = 0; tmpdiff>timediff[i] && i < alrNum ;i++)//查找到应插入的时间数组序号
  68.                 {
  69.                     j = i + 1;
  70.                 }
  71.                 switch (j)//按插入点分情况插入
  72.                 {
  73.                     case 0://当应插入数组第0位时
  74.                         arrytime[2] = arrytime[1];
  75.                         allAla[2, 0] = allAla[1, 0];
  76.                         allAla[2, 1] = allAla[1, 1];
  77.                         arrytime[1] = arrytime[0];
  78.                         allAla[1, 0] = allAla[0, 0];
  79.                         allAla[1, 1] = allAla[0, 1];
  80.                         arrytime[0] = tmpTime;
  81.                         allAla[0, 0] = tmpTime.ToLongTimeString();
  82.                         allAla[0, 1] = message;
  83.                         alrNum++;
  84.                         this.timer2.Enabled = true;
  85.                         MessageBox.Show("设置成功!");
  86.                         break;
  87.                     case 1://当应插入数组第1位时
  88.                         arrytime[2] = arrytime[1];
  89.                         allAla[2, 0] = allAla[1, 0];
  90.                         allAla[2, 1] = allAla[1, 1];
  91.                         arrytime[1] = tmpTime;
  92.                         allAla[1, 0] = tmpTime.ToLongTimeString();
  93.                         allAla[1, 1] = message;
  94.                         alrNum++;
  95.                         this.timer2.Enabled = true;
  96.                         MessageBox.Show("设置成功!");
  97.                         break;
  98.                     case 2://当应插入数组第2位时
  99.                         arrytime[2] = tmpTime;
  100.                         allAla[2, 0] = tmpTime.ToLongTimeString();
  101.                         allAla[2, 1] = message;
  102.                         alrNum++;
  103.                         this.timer2.Enabled = true;
  104.                         MessageBox.Show("设置成功!");
  105.                         break;
  106.                 }
  107.             }
  108.             
  109.         }
  110.         //定时事件设置
  111.         private void timer2_Tick(object sender, EventArgs e)
  112.         {
  113.             for (int i = 0; i < alrNum; i++)//循环查找当前定时数组中是否有定时与当前时间相同
  114.             {
  115.                 if (arrytime[i].Hour == DateTime.Now.Hour && arrytime[i].Minute == DateTime.Now.Minute && arrytime[i].Second == DateTime.Now.Second)
  116.                 {
  117.                     MessageBox.Show(allAla[i, 1]);
  118.                     alrNum--;                            //闹钟个数减1
  119.                     arrytime[i] = arrytime[alrNum];
  120.                     //arrytime[alrNum] = null;
  121.                     allAla[i, 0] = allAla[alrNum, 0];
  122.                     allAla[i, 1] = allAla[alrNum, 1];
  123.                     allAla[alrNum, 0] = null;
  124.                     allAla[alrNum, 1] = null;
  125.                 }
  126.             }
  127.         }
  128.         private void setAl_Click(object sender, EventArgs e)
  129.         {
  130.             AddTime();
  131.         }
  132.         private void delAl_Click(object sender, EventArgs e)
  133.         {
  134.             if (alrNum == 0)
  135.             {
  136.                 MessageBox.Show("没有设定闹钟!");
  137.             }
  138.             else
  139.             {
  140.                 //MessageBox.Show("确定要删除定时吗?");
  141.                 alrNum--;
  142.                 allAla[alrNum, 0] = null;
  143.                 allAla[alrNum, 1] = null;
  144.                 MessageBox.Show("删除成功!");
  145.             }
  146.             
  147.         }
  148.                /*private void showAl_Click(object sender, EventArgs e)            //test
  149.                {
  150.            
  151.                    //int a;
  152.                    //a = (int)arrytime[0].Hour - (int)DateTime.Now.Hour;
  153.                    //MessageBox.Show(a.ToString());
  154.                    MessageBox.Show(allAla[0, 0] + "," + allAla[0,1] + ";" + allAla[1, 0] + "," +allAla[1,1] + ";" +allAla[2,0]+ ","+allAla[2, 1]);
  155.                }*/
  156.         private void 当前时间ToolStripMenuItem_Click(object sender, EventArgs e)
  157.         {
  158.             timer1.Enabled = true;
  159.             timer3.Enabled = false;
  160.             this.box.Text = "Time:";
  161.             this.timelab.Text = DateTime.Now.ToLongTimeString();
  162.             this.megBox.Text = "";
  163.         }
  164.         private void 定时时间ToolStripMenuItem_Click(object sender, EventArgs e)
  165.         {
  166.             if (alrNum == 0)
  167.             {
  168.                 timer1.Enabled = false;
  169.                 this.box.Text = "Alarm:";
  170.                 this.timelab.Text = "无定时";
  171.             }
  172.             else
  173.             {
  174.                 timer1.Enabled = false;
  175.                 this.box.Text = "Alarm:";
  176.                 dateTimePicker1.Value = arrytime[0];
  177.                 this.timelab.Text = arrytime[0].ToLongTimeString();
  178.                 this.megBox.Text = allAla[0, 1];
  179.             }
  180.         }
  181.         private void FontToolStripMenuItem_Click(object sender, EventArgs e)
  182.         {
  183.             this.fontDialog1.ShowDialog();
  184.             this.Font = fontDialog1.Font;
  185.         }
  186.         private void forcolourToolStripMenuItem_Click(object sender, EventArgs e)
  187.         {
  188.             colorDialog1.ShowDialog();
  189.             this.ForeColor = colorDialog1.Color;
  190.         }
  191.         private void backcolourToolStripMenuItem_Click(object sender, EventArgs e)
  192.         {
  193.             colorDialog1.ShowDialog();
  194.             this.BackColor = colorDialog1.Color;
  195.         }
  196.         private void noToolStripMenuItem_Click(object sender, EventArgs e)
  197.         {
  198.             this.BorderStyle = BorderStyle.None;
  199.         }
  200.         private void fixedToolStripMenuItem_Click(object sender, EventArgs e)
  201.         {
  202.             this.BorderStyle = BorderStyle.FixedSingle;
  203.         }
  204.         private void fixed3DToolStripMenuItem_Click(object sender, EventArgs e)
  205.         {
  206.             this.BorderStyle = BorderStyle.Fixed3D;
  207.         }
  208.         private void trueToolStripMenuItem_Click(object sender, EventArgs e)
  209.         {
  210.             this.Visible = true;
  211.         }
  212.         private void falseToolStripMenuItem_Click(object sender, EventArgs e)
  213.         {
  214.             this.Visible = false;
  215.         }
  216.         int count = 0;
  217.         private void timer3_Tick(object sender, EventArgs e)
  218.         {
  219.             if (count < alrNum)//判断count是否超过闹钟个数
  220.             {
  221.                 dateTimePicker1.Value = arrytime[count];
  222.                 this.timelab.Text = "(" + (count+1).ToString() + ")" + arrytime[count].ToLongTimeString();
  223.                 this.megBox.Text = allAla[count, 1];
  224.                 count++;
  225.             }
  226.             else//超出时对count清零从头显示
  227.             {
  228.                 count = 0;
  229.                 dateTimePicker1.Value = arrytime[count];
  230.                 this.timelab.Text = "(" + (count + 1).ToString() + ")" + arrytime[count].ToLongTimeString();
  231.                 this.megBox.Text = allAla[count, 1];
  232.                 count++;
  233.             }
  234.         }
  235.         private void 循环显示ToolStripMenuItem_Click(object sender, EventArgs e)
  236.         {
  237.             if (alrNum == 0)
  238.             {
  239.                 timer1.Enabled = false;
  240.                 this.box.Text = "Alarm:";
  241.                 this.timelab.Text = "无定时";
  242.             }
  243.             else
  244.             {
  245.                 timer1.Enabled = false;
  246.                 this.box.Text = "Alarm:";
  247.                 timer3.Enabled = true;
  248.             }
  249.         }
  250.         private void 秒ToolStripMenuItem_Click(object sender, EventArgs e)
  251.         {
  252.             timer3.Interval = 500;
  253.         }
  254.         private void 秒ToolStripMenuItem1_Click(object sender, EventArgs e)
  255.         {
  256.             timer3.Interval = 1000;
  257.         }
  258.         private void 秒ToolStripMenuItem2_Click(object sender, EventArgs e)
  259.         {
  260.             timer3.Interval = 1500;
  261.         }
  262.         private void 秒ToolStripMenuItem3_Click(object sender, EventArgs e)
  263.         {
  264.             timer3.Interval = 2000;
  265.         }
  266.     }
  267. }