码迷,mamicode.com
首页 > 其他好文 > 详细

秒表的设计

时间:2015-02-28 18:08:45      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

namespace WindowsFormsApplication1
{
    public partial class Stopwatch : Form
    {
        /// <summary>
        /// 定义变量
        /// </summary>
        int record_index = 1;//定义record-index变量为1
        private DateTime startTime;

        public Stopwatch()
        {
            InitializeComponent(); 
        }
        /// <summary>
        /// 为窗体FormSW的Load事件增加事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormSW_Load(object sender, EventArgs e)
        {
            this.tmrStopWatch.Interval = 50;//每隔0.05秒触发一个计时器事件
            this.tmrStopWatch.Enabled = false;//计时器停止工作
        }
        /// <summary>
        ///  每0.05秒钟更新一次当前时间,由Timer控件的Tick事件完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tmrStopWatch_Tick(object sender, EventArgs e)
        {
            TimeSpan diff = DateTime.Now - startTime;//当前时间减去开始的时间得到的时间差
            DateTime show_time = new DateTime(diff.Ticks);//将时间差显示出来
            lblStopWatch.Text = show_time.ToString("HH : mm : ss : fff");//将时间差显示到标签文本中
        }
        /// <summary>
        /// 响应控件btnBegin事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBegin_Click(object sender, EventArgs e)
        {
            if (btnBegin.Text == "开始")
            {
                startTime = DateTime.Now;//将现在的时间付给开始的时间
                tmrStopWatch.Start();//计时器开始工作
                btnBegin.Text = "停止";
            }
            else
            {
                if (btnBegin.Text == "停止")
                {
                    tmrStopWatch.Stop();
                    btnBegin.Text = "继续";
                }
                else
                {
                    tmrStopWatch.Start();
                    btnBegin.Text = "停止";
                }
            }
        }
        private void btnZero_Click(object sender, EventArgs e)
        {
            if (tmrStopWatch.Enabled)
            {
                tmrStopWatch.Stop();
            }
            System.DateTime TimeP = new System.DateTime(0);//系统时间从零开始
            lblStopWatch.Text = TimeP.ToString("HH : mm : ss : fff");//将时间显示到lblStopWatch标签框中
            btnBegin.Text = "开始";

        }
        /// <summary>
        /// 响应控件btnRecord事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRecord_Click(object sender, EventArgs e)
        {
            if (record_index == 1)
            {
                textBox1.Text = lblStopWatch.Text;
            }
            else if (record_index == 2)
            {
                textBox2.Text = lblStopWatch.Text;
            }
            else if (record_index == 3)
            {
                textBox3.Text = lblStopWatch.Text;
            }
            else if (record_index == 4)
            {
                textBox4.Text = lblStopWatch.Text;
            }

            record_index++;//变量叠加
            if (record_index == 5)//假设record为5,则返回为1
            {
                record_index = 1;
            }
        }
    }
}
在这个小的应用程序中,我学会的是如何刷新定时器得到时间差。这是一个时间片段。如下:

TimeSpan diff = DateTime.Now - startTime;//当前时间减去开始的时间得到的时间差
DateTime show_time = new DateTime(diff.Ticks);//将时间差显示出来
lblStopWatch.Text = show_time.ToString("HH : mm : ss : fff");//将时间差显示到标签文本中

在time控件Tick事件里响应出来。

以及如何定义变量实现循环。

 

秒表的设计

标签:

原文地址:http://www.cnblogs.com/qh123/p/4305794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!