刷新计时器c #Windows Forms

时间:2015-09-15 09:49:54

标签: c# timer refresh

Hy,每次按下“开始”按钮,我都需要将定时器值刷新为0。请帮忙。

这是我的代码:

    namespace Timer
    {
        public partial class Form1 : Form
        {
            int counter = 0;
            bool counting = true;

            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                timer.Interval = 1000;
            }

            private void btnStart_Click(object sender, EventArgs e)
            {
                if (timer.Enabled)
                {
                    timer.Stop();
                    btnStart.Text = "START";
                    lbTime.Items.Add("Time:" + lbCounter.Text);
                    lbCounter.Text = "";
                }
                else
                {
                    timer.Start();
                    btnStart.Text = "STOP";
                }
            }

            private void timer_Tick(object sender, EventArgs e)
            {
                Count();
            }

            private void Count()
            {
                counter++;
                lbCounter.Text = counter.ToString();
            }

        }
    }

当我按下开始按钮时,计时器从0开始计数。当我再按一次时它会停止。第三次它应该从0开始,但它会从上次计数停止的地方继续。

2 个答案:

答案 0 :(得分:0)

停止计时器时,只需将计数器设置为0:

   private void btnStart_Click(object sender, EventArgs e)
   {
        if (timer.Enabled)
        {
            timer.Stop();
            btnStart.Text = "START";
            lbTime.Items.Add("Time:" + lbCounter.Text);
            counter = 0;
            lbCounter.Text = "";
        }
        else
        {
            timer.Start();
            btnStart.Text = "STOP";
        }
    }

答案 1 :(得分:0)

if (timer.Enabled)
           {
                timer.Stop();
                btnStart.Text = "START";
                lbTime.Items.Add("Time:" + lbCounter.Text);
                lbCounter.Text = "";
            }
            else
            {
                counter = 0; // Added code.
                timer.Start();
                btnStart.Text = "STOP";
            }