1

在 form1 的顶部,我有:

private int seconds;
private int minutes;
private int hours;

在构造函数中:

seconds = 0;
minutes = 20;
hours = 0;
label9.Visible = false;
label9.Text = "00:00:00";

然后是计时器 3 滴答事件。我在 timer3 滴答事件上使用了一个断点,我看到分钟和秒正在倒数,但标签 9 没有更新。

也许 label9 上的 string.Format 不正确?

private void timer3_Tick(object sender, EventArgs e)
        {
            // Verify if the time didn't pass.
            if ((minutes == 0) && (hours == 0) && (seconds == 0))
            {
                // If the time is over, clear all settings and fields.
                // Also, show the message, notifying that the time is over.
                timer3.Enabled = false;
                label9.Visible = true;
                label9.Text = "00:00:00";
            }
            else
            {
                // Else continue counting.
                if (seconds < 1)
                {
                    seconds = 59;
                    if (minutes == 0)
                    {
                        minutes = 59;
                        if (hours != 0)
                            hours -= 1;

                    }
                    else
                    {
                        minutes -= 1;
                    }
                }
                else
                    seconds -= 1;
                // Display the current values of hours, minutes and seconds in
                // the corresponding fields.
                label7.Visible = true;
                label9.Visible = true;
                label9.Text = string.Format("{00} : {00} : {00}", hours.ToString(), minutes.ToString(),seconds.ToString());
            }
        }

我最后在标签 9 上看到的只是:0:0:0 就是这样,我没有看到倒数 20 分钟。

4

1 回答 1

3

您需要修复字符串格式,这是罪魁祸首:

label9.Text = string.Format("{0:00} : {1:00} : {2:00}", hours, minutes, seconds);

另外 - 考虑使用其他一些机制(例如Stopwatch类)来计算经过的时间;您不能保证所编写的代码将每 1000 毫秒准确执行一次。

于 2013-11-14T09:36:18.417 回答