倒数计时器自动启动

Posted

技术标签:

【中文标题】倒数计时器自动启动【英文标题】:Countdown timer automatically starts 【发布时间】:2015-12-20 05:27:24 【问题描述】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace SoftwareEngineering

    public partial class MainGame : Form
    
        private int tick = 60;

        public MainGame()
        
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        
        private void MainGame_Load(object sender, EventArgs e)
        

        
        private void NewGame_Click(object sender, EventArgs e)
        
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
        
        private void timer1_Tick(object sender, EventArgs e)
        
            timeremaining.Text = tick + " Remaining";
            if(tick > 0)
            
                tick--;
            
            else
            
                timeremaining.Text = "Times Up!";
            
        

        private void timeremaining_TextChanged(object sender, EventArgs e)
        

        

我的计时器有问题,当我启动程序时,计时器会自动倒计时而不按按钮,如果单击按钮计时器会减1。

示例: “56 Remaining”当我单击按钮时,计时器将递减,结果将是“54 Remaining”,依此类推。 (单击按钮)从“54 到 51”。

我该如何解决这个问题,请帮忙。

【问题讨论】:

您是否在计时器上将Enabled 设置为True?将其更改为False @Blorgbeard 甚至在我之前就给出了正确答案。我可能在打字时错过了它。他的回答值得称赞。 @vnikhil 但我很懒,只是发表了评论;你实际上花时间写了一个答案:你得到了信用:) @Blorgbeard 如此谦虚(鞠躬) 【参考方案1】:

看起来您在表单设计器上使用了 Timer 控件并启用了它。这会导致它在表单加载后立即开始滴答作响。让它为假。

【讨论】:

以上是关于倒数计时器自动启动的主要内容,如果未能解决你的问题,请参考以下文章

MVC 中的倒数计时器

Flutter:当倒数计时器达到 0 时执行动作

在列表视图中添加倒数计时器会在每个新行中重新启动

如何从 Android 应用程序与服务器同步倒数计时器

管理员单击按钮后为所有用户启动倒数计时器(react、redux、socket.io)

倒数计时器停止在后台运行