C# Windows 窗体倒计时器

标签 c# winforms timer countdown

我有 3 个文本框(小时、分钟、秒)、一个开始、暂停和停止按钮,我使用标签来显示当前计时器。我还有一个间隔为 1000 的计时器。我的问题是为什么不使用标签显示时间?其他一切都有效,只是不会注册我已将值放入文本框中。

代码

启动计时器:

    private void startbutton_Click(object sender, EventArgs e)
    {
        if (paused != true) //Timer is not paused (active)
        {

            int.TryParse(textBoxhrs.Text, out hours);
            int.TryParse(textBoxmin.Text, out minutes);
            int.TryParse(textBoxsec.Text, out seconds);

            if (hours >= 1 || (minutes >= 1) || (seconds >= 1))

            //If there is at least one integer entered in any of the 3 boxes, executes; else - //throws an exception
            {
                startbutton.Enabled = true;
                pausebutton.Enabled = true; //changed the value to 'true'  
                stopbutton.Enabled = true; //changed the value to 'true'
                textBoxhrs.Enabled = false;
                textBoxmin.Enabled = false;
                textBoxsec.Enabled = false;
            }
            else
            {
                MessageBox.Show("Enter at least one integer!");
            }
        }
    }

    private void stopbutton_Click(object sender, EventArgs e)
    {
        // Stop the timer. 
        paused = false;
        timer1.Enabled = false;
        startbutton.Enabled = true; //changed to true
        stopbutton.Enabled = false; //changed to false
        pausebutton.Enabled = false; //changed to false
        textBoxsec.Clear();
        textBoxmin.Clear();
        textBoxhrs.Clear();
        textBoxhrs.Enabled = true;
        textBoxsec.Enabled = true;
        textBoxmin.Enabled = true;
        textBoxhrs.Enabled = true;
        lblHr1.Text = "00";
        lblMin1.Text = "00";
        lblSec1.Text = "00";
        MessageBox.Show("Timer is Stopped, to re-start press <Start>"); //updated to give user a chance to run the timer again after stoppage.

    }

暂停按钮:

    private void pausebutton_Click(object sender, EventArgs e)
    {
        // Pause the timer. 
        timer1.Enabled = false;
        paused = true; //
        startbutton.Enabled = true; // changed to true
        pausebutton.Enabled = false; //changed to false
    }

计时器:

    private void timer1_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.
            timer1.Enabled = false;
            MessageBox.Show(textBoxMsg.Text);
            pausebutton.Enabled = false;
            stopbutton.Enabled = false;
            startbutton.Enabled = true;
            textBoxMsg.Clear();
            textBoxsec.Clear();
            textBoxmin.Clear();
            textBoxhrs.Enabled = true;
            textBoxMsg.Enabled = true;
            textBoxsec.Enabled = true;
            textBoxmin.Enabled = true;
            textBoxhrs.Enabled = true;
            lblHr1.Text = "00";
            lblMin1.Text = "00";
            lblSec1.Text = "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.
            lblHr1.Text = hours.ToString();
            lblMin1.Text = minutes.ToString();
            lblSec1.Text = seconds.ToString();
        }
    }

最佳答案

您需要做两件事才能使其正常工作:

您需要在startbutton_Click中启动计时器:

if (hours >= 1 || (minutes >= 1) || (seconds >= 1))

//If there is at least one integer entered in any of the 3 boxes, executes; else - //throws an exception
{
    startbutton.Enabled = true;
    ...
    timer1.Enabled = true;
}

您需要将计时器Tick事件连接到timer1_Tick。您可以通过选择计时器并单击“属性”框架上的闪电工具栏图标,然后选择“timer1_Tick”作为“Tick”来完成此操作。

关于C# Windows 窗体倒计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10576024/

相关文章:

c# - 在控件集合中找不到上下文菜单

c# - 每秒运行一次函数 Visual C#

java - 比游戏循环更快地检查按键

javascript - 使用计时器清理 Socket.io session 数据

c# - 如何在 WPF 中使用关闭按钮关闭选项卡?

c# - C#中字典的内存使用

c# - Web 应用程序与 Linux 多用户应用程序

c# - 如何从另一个表单更改标签的文本?

c# - 使用资源文件的多语言支持

c# - 尝试将依赖项注入(inject)与 XUnit 一起使用时出现未解析的构造函数参数错误