using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1 = "";
            button2.Enabled = false;
            button3.Enabled = false;
        }        int h = 0;
        int m = 0;
        int s = 0;
        int ms = 0;//毫秒        private void button3_Click(object sender, EventArgs e)//结束按钮
        {
            timer1.Enabled = false;
            button1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;            textBox1.Text = "";
            listBox1.Items.Clear();
        }        private void button1_Click_1(object sender, EventArgs e)//开始按钮
        {
            textBox1 = "0:0.0 0";
            listBox1.Items.Clear();
            timer1.Enabled = true;
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = true;
        }        private void button2_Click_1(object sender, EventArgs e)//记录按钮
        {
            listBox1.Items.Add(textBox1.text);
        }        private void timer1_Tick_1(object sender, EventArgs e)//时钟控件,频率为 1 毫秒
        {
            if (ms < 999)
                ms++;
            else
            {
                ms = 0;
                if (s < 59)
                    s++;
                else
                {
                    s = 0;
                    if (m < 59)
                        m++;
                    else
                    {
                        m = 0;
                        h++;
                    }
                }
            }            textBox1.Text = "" + h + ":" + m + "." + s + " " + ms + "";//小时: 分. 秒 毫秒
        }    }
}不知为何,显示结果极不准确……

解决方案 »

  1.   

    别用timer统计,它是不可能精确的,特殊时候(繁忙)还是要丢帧(时间片)的(名言:过去的就让它过去吧)开始的时候记录dateTime.Now  timeStarttimer里面得到当前时间,然后减去timeStart得到时间差timeSpan,再统计数据不要简单的++--
      

  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;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            DateTime dtbegin = DateTime.MinValue;        public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = "0:0.0 0";
                listBox1.Items.Clear();
                timer1.Enabled = true;
                button1.Enabled = false;
                button2.Enabled = true;
                button3.Enabled = true;            if (this.dtbegin == DateTime.MinValue)
                {
                    this.dtbegin = DateTime.Now;
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                dtbegin = DateTime.MinValue;            timer1.Enabled = false;
                button1.Enabled = true;
                button2.Enabled = false;
                button3.Enabled = false;            textBox1.Text = "";
                listBox1.Items.Clear();
                
            }        private void button3_Click(object sender, EventArgs e)
            {
                listBox1.Items.Add(textBox1.Text);
            }        private void Form1_Load(object sender, EventArgs e)
            {
                this.textBox1.Text="";
                button2.Enabled = false;
                button3.Enabled = false;
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                TimeSpan tstemp = DateTime.Now - this.dtbegin;
                this.textBox1.Text = tstemp.Hours.ToString()+":" + tstemp.Minutes.ToString()+":" + tstemp.Seconds.ToString()+":" + tstemp.Milliseconds.ToString();
            }
        }
    }
      

  3.   

    这里是我上传的源码,你可以看看
    http://download.csdn.net/detail/zlcoolzl/4260786
      

  4.   


    if (this.dtbegin == DateTime.MinValue)
    为什么要加这句?