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

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

【技术要点】

         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();
        }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值