不要关闭的多事件提醒小闹铃C#初学者(三)

来源:互联网 发布:spss clementine mac 编辑:程序博客网 时间:2024/06/05 02:00

不要关闭的多事件提醒小闹铃

【技术要点】

         SortedList类,可自动排序的字典集合类型。

                   Add(objectkey, object value) 以键值对应形式添加新元素

                   Remove(objectkey)移除指定键的无素

                   ContainsKey(Objectkey)确定是否包含特定键

【实现步骤】

——界面

         打开WindowsFormsApplication1项目在Form1窗体中添加button控件,修改text属性为规则。

——代码

         建立两个公共变量

         public SortedList<string, object> rule= new SortedList<string, object>();//声明存储规则的字典集合

     public stringcurrent_time;//存储当前时间值

         在button1_Click事件中加入代码从文本框中获取规则

         rule.Add(textBox1.Text.ToString(),textBox2.Text.ToString());//获取规则加入字典集合

         改写timer1_Tick事件

         current_time = DateTime.Now.ToString();//保存当前时间

            //比较当前与目标时间

            if(rule.ContainsKey(current_time)){

                timer1.Enabled = false;//暂停timer1

                timer2.Enabled = true; }//启动timer2

            else{label1.Text= current_time ; }//在label1显示当前时间

      并相应改写托盘气泡提示信息来源为rule[current_time].ToString()

——完善

         将代码button1.Text = "规则" + rule.Count.ToString();分别加入到Form1_Load、notifyIcon1_BalloonTipClicked、button1_Click事件中实现规则数量显示。

——改进

         程序重启后待处理事件保存提醒

【主要代码】

 public static bool flag = false;//表示托盘图标是否闪烁
        public SortedList<string, object> rule = new SortedList<string, object>();//声明存储规则的字典集合
        public string current_time;//存储当前时间值
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //比较当前与目标时间
            if (rule.ContainsKey(DateTime.Now.ToString()))//确定是否包含特定键
            {
                timer1.Enabled = false;//暂停timer1
                current_time = DateTime.Now.ToString ();//保存当前时间
                timer2.Enabled = true;//启动timer2
            }
            else
            {
                label1.Text = DateTime.Now.ToString();//在label1显示当前时间
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = DateTime.Now.ToString ()+".";//用符号区别输入
            textBox2.Text = "_";//防止空字符
            timer1.Enabled = true;//启动timer1
            button1.Text = "规则" + rule.Count.ToString();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //比较托盘图标标志
            if (flag == false)
            {
                notifyIcon1.Visible =true;//托盘图标显示
                notifyIcon1.ShowBalloonTip(1000, "时间到",rule[current_time].ToString (), ToolTipIcon.Info);//任务栏气泡提示
                flag = true;
            }
            else
            {
                notifyIcon1.Visible =false;//托盘图标隐藏
                flag = false;
            }
        }

        private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
        {
            rule.Remove(current_time);
            button1.Text = "规则" + rule.Count.ToString();
            timer1.Enabled = true;//启动timer1
            timer2.Enabled = false;//暂停timer2
        }

        private void button1_Click(object sender, EventArgs e)
        {
            rule.Add(textBox1.Text.ToString(), textBox2.Text.ToString());//获取规则加入字典集合
            button1.Text = "规则" + rule.Count.ToString();
        }

 

原创粉丝点击