UserControl1.cs
资源名称:alarm.rar [点击查看]
上传用户:sz2001
上传日期:2022-07-12
资源大小:85k
文件大小:10k
源码类别:
C#编程
开发平台:
C#
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
- namespace alarm
- {
- public partial class alarm : UserControl
- {
- [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
- public class Author : Attribute
- {
- public Author(string name,double versn)
- {
- myName = name;
- version = versn;
- }
- public double version;
- string myName;
- public string GetName()
- {
- return myName;
- }
- }
- //设置变量
- [Author("欧阳昭暐", 1.0)]
- DateTime tmpTime;//记录用户设置的临时时间变量
- string message;//记录用户设置的提示信息
- DateTime[] arrytime = new DateTime[3];//时间数组记录已设置的多个定时
- string[,] allAla = new string[3,2];//二维数组记录已设置的时间字串和提示信息字符串
- int alrNum = 0;//记录已设置的闹钟个数
- int tmpdiff;//记录当前定时差值临时变量
- int[] timediff = new int[3];//记录已设置闹钟的定时差值
- [Author("欧阳昭暐",1.0)]
- public alarm()
- {
- InitializeComponent();
- }
- //显示当前时间
- private void timer1_Tick(object sender, EventArgs e)
- {
- this.timelab.Text = DateTime.Now.ToLongTimeString();
- }
- //设置闹钟
- [Author("欧阳昭暐", 1.0)]
- void AddTime()
- {
- if (alrNum >= 3)//判断是否超过定时器上限
- {
- MessageBox.Show("对不起,定时器已满~");
- }
- else
- {
- if (this.megBox.Text == "")//判断用户是否输入了提示信息
- {
- MessageBox.Show("请输入提示信息!");
- return;
- }
- tmpTime = this.dateTimePicker1.Value;//记录定时时间
- message = this.megBox.Text;//记录定时提示信息
- 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);
- //记录当前时间差值
- int j = 0;//记录已设置定时的时间差
- 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);
- for (int i = 0; tmpdiff>timediff[i] && i < alrNum ;i++)//查找到应插入的时间数组序号
- {
- j = i + 1;
- }
- switch (j)//按插入点分情况插入
- {
- case 0://当应插入数组第0位时
- arrytime[2] = arrytime[1];
- allAla[2, 0] = allAla[1, 0];
- allAla[2, 1] = allAla[1, 1];
- arrytime[1] = arrytime[0];
- allAla[1, 0] = allAla[0, 0];
- allAla[1, 1] = allAla[0, 1];
- arrytime[0] = tmpTime;
- allAla[0, 0] = tmpTime.ToLongTimeString();
- allAla[0, 1] = message;
- alrNum++;
- this.timer2.Enabled = true;
- MessageBox.Show("设置成功!");
- break;
- case 1://当应插入数组第1位时
- arrytime[2] = arrytime[1];
- allAla[2, 0] = allAla[1, 0];
- allAla[2, 1] = allAla[1, 1];
- arrytime[1] = tmpTime;
- allAla[1, 0] = tmpTime.ToLongTimeString();
- allAla[1, 1] = message;
- alrNum++;
- this.timer2.Enabled = true;
- MessageBox.Show("设置成功!");
- break;
- case 2://当应插入数组第2位时
- arrytime[2] = tmpTime;
- allAla[2, 0] = tmpTime.ToLongTimeString();
- allAla[2, 1] = message;
- alrNum++;
- this.timer2.Enabled = true;
- MessageBox.Show("设置成功!");
- break;
- }
- }
- }
- //定时事件设置
- private void timer2_Tick(object sender, EventArgs e)
- {
- for (int i = 0; i < alrNum; i++)//循环查找当前定时数组中是否有定时与当前时间相同
- {
- if (arrytime[i].Hour == DateTime.Now.Hour && arrytime[i].Minute == DateTime.Now.Minute && arrytime[i].Second == DateTime.Now.Second)
- {
- MessageBox.Show(allAla[i, 1]);
- alrNum--; //闹钟个数减1
- arrytime[i] = arrytime[alrNum];
- //arrytime[alrNum] = null;
- allAla[i, 0] = allAla[alrNum, 0];
- allAla[i, 1] = allAla[alrNum, 1];
- allAla[alrNum, 0] = null;
- allAla[alrNum, 1] = null;
- }
- }
- }
- private void setAl_Click(object sender, EventArgs e)
- {
- AddTime();
- }
- private void delAl_Click(object sender, EventArgs e)
- {
- if (alrNum == 0)
- {
- MessageBox.Show("没有设定闹钟!");
- }
- else
- {
- //MessageBox.Show("确定要删除定时吗?");
- alrNum--;
- allAla[alrNum, 0] = null;
- allAla[alrNum, 1] = null;
- MessageBox.Show("删除成功!");
- }
- }
- /*private void showAl_Click(object sender, EventArgs e) //test
- {
- //int a;
- //a = (int)arrytime[0].Hour - (int)DateTime.Now.Hour;
- //MessageBox.Show(a.ToString());
- MessageBox.Show(allAla[0, 0] + "," + allAla[0,1] + ";" + allAla[1, 0] + "," +allAla[1,1] + ";" +allAla[2,0]+ ","+allAla[2, 1]);
- }*/
- private void 当前时间ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- timer1.Enabled = true;
- timer3.Enabled = false;
- this.box.Text = "Time:";
- this.timelab.Text = DateTime.Now.ToLongTimeString();
- this.megBox.Text = "";
- }
- private void 定时时间ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (alrNum == 0)
- {
- timer1.Enabled = false;
- this.box.Text = "Alarm:";
- this.timelab.Text = "无定时";
- }
- else
- {
- timer1.Enabled = false;
- this.box.Text = "Alarm:";
- dateTimePicker1.Value = arrytime[0];
- this.timelab.Text = arrytime[0].ToLongTimeString();
- this.megBox.Text = allAla[0, 1];
- }
- }
- private void FontToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.fontDialog1.ShowDialog();
- this.Font = fontDialog1.Font;
- }
- private void forcolourToolStripMenuItem_Click(object sender, EventArgs e)
- {
- colorDialog1.ShowDialog();
- this.ForeColor = colorDialog1.Color;
- }
- private void backcolourToolStripMenuItem_Click(object sender, EventArgs e)
- {
- colorDialog1.ShowDialog();
- this.BackColor = colorDialog1.Color;
- }
- private void noToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.BorderStyle = BorderStyle.None;
- }
- private void fixedToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.BorderStyle = BorderStyle.FixedSingle;
- }
- private void fixed3DToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.BorderStyle = BorderStyle.Fixed3D;
- }
- private void trueToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Visible = true;
- }
- private void falseToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Visible = false;
- }
- int count = 0;
- private void timer3_Tick(object sender, EventArgs e)
- {
- if (count < alrNum)//判断count是否超过闹钟个数
- {
- dateTimePicker1.Value = arrytime[count];
- this.timelab.Text = "(" + (count+1).ToString() + ")" + arrytime[count].ToLongTimeString();
- this.megBox.Text = allAla[count, 1];
- count++;
- }
- else//超出时对count清零从头显示
- {
- count = 0;
- dateTimePicker1.Value = arrytime[count];
- this.timelab.Text = "(" + (count + 1).ToString() + ")" + arrytime[count].ToLongTimeString();
- this.megBox.Text = allAla[count, 1];
- count++;
- }
- }
- private void 循环显示ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (alrNum == 0)
- {
- timer1.Enabled = false;
- this.box.Text = "Alarm:";
- this.timelab.Text = "无定时";
- }
- else
- {
- timer1.Enabled = false;
- this.box.Text = "Alarm:";
- timer3.Enabled = true;
- }
- }
- private void 秒ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- timer3.Interval = 500;
- }
- private void 秒ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- timer3.Interval = 1000;
- }
- private void 秒ToolStripMenuItem2_Click(object sender, EventArgs e)
- {
- timer3.Interval = 1500;
- }
- private void 秒ToolStripMenuItem3_Click(object sender, EventArgs e)
- {
- timer3.Interval = 2000;
- }
- }
- }