基本信息
源码名称:C# 双模式闹钟 实例源码(定时响铃)
源码大小:1.61M
文件格式:.rar
开发语言:C#
更新时间:2017-01-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.Text.RegularExpressions;

namespace _12Timer加Label加DateTime.Now加播放WAV做闹钟
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 为时钟附初值,为定时闹钟的时分秒赋初值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            this.label1.Text = "当前时间:"   DateTime.Now.ToString();//年月日时分秒
            //初始化时钟闹铃
            textBox2.Text = DateTime.Now.Hour.ToString();//时
            textBox3.Text = (DateTime.Now.Minute 10).ToString();//分 10分钟后
            textBox4.Text = DateTime.Now.Second.ToString();//秒
            //初始化倒计时秒数
            InitSeconds();
        }
        /// <summary>
        /// 从上面提取的方法 因为多处用 提取一下,提高代码复用性
        /// </summary>
        private void InitSeconds()
        {            
            if (radioButton1.Checked)//选中小时
            {                
                secondCount = Convert.ToInt32(textBox1.Text) * 60 * 60;//加个正则表达式限制必须为数字
                label2.Text = secondCount.ToString()   "秒";
            }
            else if (radioButton2.Checked)
            {
                secondCount = Convert.ToInt32(textBox1.Text) * 60;
                label2.Text = secondCount.ToString()   "秒";
            }
            else if (radioButton3.Checked)
            {
                secondCount = Convert.ToInt32(textBox1.Text);
                label2.Text = secondCount.ToString()   "秒";
            }
            this.textBox1.Focus();
        }
        /// <summary>
        /// 倒计时闹钟
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            secondCount--;
            if (secondCount < 0)
            {
                this.timer1.Enabled = false;
                this.label2.Text = "倒计时结束啦!!!";//后改剩时分秒加
                //SoundPlayer sp = new SoundPlayer();
                //sp.SoundLocation = @"C:\Documents and Settings\Administrator\桌面\1.wav";
                sp.SoundLocation = @"wav\1.wav";
                sp.Play();
                return;
            }
            else
            {
                //this.label2.Text = secondCount.ToString()   "秒";//Label2为
                this.label2.Text = GetHmsByS(secondCount);
            }
        }
        /// <summary>
        /// 将剩余秒数转换成几小时几分几秒
        /// </summary>
        /// <param name="second"></param>
        /// <returns></returns>
        public static string GetHmsByS(int second)
        {
            int h, m, s;
            h = second / 3600;
            m = (second % 3600) / 60;
            s = second % 60;
            string res = "剩余"   h   "小时"   m   "分钟"   s   "秒";//这里有个隐式转换
            return res;
        }
        /// <summary>
        /// 倒计时启动按钮 启动Timer1控件 倒计时为0响铃
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (label2.Text.Trim().ToUpper() == "S")
            {
                MessageBox.Show("未生成有效的倒计时秒数...");
                return;
            }
            else
            {
                if (this.button1.Text == "启动倒计时闹钟")
                {
                    this.button1.Text = "停止倒计时闹钟";
                    this.timer1.Enabled = true;
                }
                else
                {
                    this.button1.Text = "启动倒计时闹钟";                    
                    this.radioButton2.Checked = true;
                    this.textBox1.Text = "10";
                    //this.label2.Text = "S";
                    InitSeconds();
                    this.timer1.Enabled = false;
                    sp.Stop();
                }
            }
        }

        int hour, min, sec;//声明三个字段 类下(此项目下)所有方法调用的 时 分 秒
        /// <summary>
        /// 时钟闹钟启动按钮 启用Timer2控件 用于几点几分几秒响铃
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.button2.Text == "启动时间闹钟")
            {
                this.button2.Text = "停止时间闹钟";
                this.label10.Text = "计时中...";//为结束提示后加
                hour = Convert.ToInt32(textBox2.Text.Trim());
                min = Convert.ToInt32(textBox3.Text.Trim());
                sec = Convert.ToInt32(textBox4.Text.Trim());
                this.timer2.Enabled = true;
            }
            else
            {
                this.button2.Text = "启动时间闹钟";
                this.label10.Text = "?";//为结束提示后加
                textBox2.Text = DateTime.Now.Hour.ToString();
                textBox3.Text = (DateTime.Now.Minute 10).ToString();
                textBox4.Text = DateTime.Now.Second.ToString();
                this.timer2.Enabled = false;
                sp.Stop();
            }

        }

        SoundPlayer sp = new SoundPlayer();//==================
        /// <summary>
        /// 时间闹钟
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Hour == hour && DateTime.Now.Minute == min && DateTime.Now.Second == sec)
            {
                this.timer2.Enabled = false;
                this.label10.Text = "到时间啦!!!";//为到点结束提示后加
                sp.SoundLocation = @"wav\1.wav";
                sp.Play();
            }
        }
        /// <summary>
        /// Timer3默认就启用 1000毫秒执行一次 用于控制最上面Label1显示的时钟
        /// 而Timer1和Timer2默认是不启用的 由按钮启用 用于控制闹钟
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer3_Tick(object sender, EventArgs e)
        {
            label1.Text = "当前时间:"   DateTime.Now.ToString();
        }

        int secondCount = 0;//声明一个字段(默认private) 用于记录倒计时秒数
        /// <summary>
        /// 设置秒数 并给 倒计时显示的Label2 并为倒计时秒数的私有字段赋初值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            #region 正则表达式 方式 限制为 数字
            //bool b = Regex.IsMatch(textBox1.Text, @"^(\d )$");//匹配数字
            //if (!b)
            //{
            //    MessageBox.Show("时间设置必须为数字...");
            //    return;
            //} 
            #endregion
            try
            {
                secondCount = Convert.ToInt32(this.textBox1.Text) * 60;
                this.label2.Text = secondCount.ToString()   "秒";
            }
            catch
            {
                MessageBox.Show("输入非法...");
                return;
            }
        }

        /// <summary>
        /// 时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                secondCount = Convert.ToInt32(textBox1.Text) * 60 * 60;
                label2.Text = secondCount.ToString()   "秒";
            }
        }
        /// <summary>
        /// 分
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                secondCount = Convert.ToInt32(textBox1.Text) * 60;
                label2.Text = secondCount.ToString()   "秒";
            }
        }
        /// <summary>
        /// 秒
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                secondCount = Convert.ToInt32(textBox1.Text);
                label2.Text = secondCount.ToString()   "秒";
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int tmp=Convert.ToInt32(textBox2.Text.Trim());
            }
            catch 
            {
                MessageBox.Show("输入非法...");
                textBox2.Focus();
                return;
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int tmp = Convert.ToInt32(textBox3.Text.Trim());
            }
            catch
            {
                MessageBox.Show("输入非法...");
                textBox3.Focus();
                return;                
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int tmp = Convert.ToInt32(textBox4.Text.Trim());
            }
            catch
            {
                MessageBox.Show("输入非法...");
                textBox4.Focus();
                return;                
            }
        }
    }
}

//本可推广出定时器似秦一中简...小KS