【问题标题】: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 如此谦虚(鞠躬)

标签: c# winforms timer


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多