0

我想知道如何创建一个sum + 1每 5 秒添加一次的标签?我尝试过使用 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);
        }
    }
}
4

3 回答 3

4

sum应该是一个类字段。您也可以为经过的时间跨度使用自定义格式字符串。

    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 毫秒,那么您可以在每个刻度上增加 sum,但设置label2.Text = (sum % 5).ToString().

于 2013-01-09T00:46:36.257 回答
1

every time your stopwatch TICKS, sum is inside TICK and it will reset and start from

int sum=0;

so try to make sum variable GLOBAL outside timer1_Tick event and it will continue increasing.

于 2013-01-09T00:51:22.513 回答
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"; 
        }
    }
于 2013-01-09T00:48:51.790 回答