将sum添加到计时器

时间:2013-01-09 00:43:54

标签: c# timer sum

我想知道我是如何创建一个每5秒增加'sum +1'的标签?我尝试过使用if循环但不幸的是它会在一秒后重置。 感谢您的关注

`

using System.Diagnostics;
// using system.diagnotics voor stopwatch 

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private Stopwatch sw = new Stopwatch();

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            sw.Start();

            if (timer1.Enabled == true) { button1.Text = "stop"; }
            else { button1.Text = "false"; sw.Stop(); }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int hours = sw.Elapsed.Hours;
            int minutes = sw.Elapsed.Minutes;
            int seconds = sw.Elapsed.Seconds;
            int sum = 0; 
            label1.Text = hours + ":" ;
            if (minutes < 10) { label1.Text += "0" + minutes + ":"; }
            else { label1.Text += minutes + ":"; }

            if (seconds < 10) { label1.Text += "0" + seconds ; }
            else { label1.Text += seconds ; }

            if (seconds ==5) { sum = sum +=1; }


            label2.Text = Convert.ToString(sum);
        }
    }
}` 

3 个答案:

答案 0 :(得分:4)

sum应该是一个类字段。此外,您可以使用自定义格式字符串表示已过时的TimeSpan。

    int sum = 0;

    private void timer1_Tick(object sender, EventArgs e)
    {
        // int sum = 0; local variable will be set to zero on each timer tick
        label1.Text = sw.Elapsed.ToString(@"hh\:mm\:ss");

        // btw this will not update sum each five seconds
        if (sw.Elapsed.Seconds == 5) 
            sum++; // same as sum = sum +=1;

        label2.Text = sum.ToString();
    }

仅当当前经过的超时的第二个值为5时,您当前的实现才会增加总和。这可能永远不会发生(取决于您的计时器间隔)。如果您将计时器间隔设置为1000毫秒,则可以在每个刻度上增加总和,但设置label2.Text = (sum % 5).ToString()

答案 1 :(得分:1)

每次计时器过去时,您必须将sum移出计时器回调,因为您将其设置为0

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private int sum = 0;
    private DateTime lastUpdate;
    private Stopwatch sw = new Stopwatch();

    private void timer1_Tick(object sender, EventArgs e)
    {
        label1.Text = string.Format("{0:00}:{1:00}:{2:00}",
                      sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds);

        if (DateTime.Now >= lastUpdate.AddSeconds(5))
        {
            sum++;
            lastUpdate = DateTime.Now;
            label2.Text = sum.ToString();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled == true)
        {
            sw.Stop(); 
            button1.Text = "stop";
        }
        else 
        {
            sum = 0;
            lastUpdate = DateTime.Now;
            timer1.Enabled = true;
            sw.Start();
            button1.Text = "Start"; 
        }
    }

答案 2 :(得分:1)

每当你的秒表TICKS,总和在TICK内,它将重置并从

开始
int sum=0;

所以尝试在timer1_Tick事件之外创建sum变量GLOBAL并且它将继续增加。