简体   繁体   English

倒数计时器自动启动

[英]Countdown timer automatically starts

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)
        {

        }

I have a problem with my timer, When I start the program, the timer will automatically countdown without pressing the button, and If click the button the timer decrements by 1. 我的计时器有问题,当我启动程序时,计时器将自动倒计时,而无需按按钮;如果单击按钮,计时器将递减1。

Example: "56 Remaining" when I clicked the button the timer will decrement and the result will be "54 Remaining" and so on. 示例:当我单击按钮时,“ 56剩余”将减少计时器,结果将为“ 54剩余”,依此类推。 (Button is clicked) from "54 to 51". (单击按钮)从“ 54至51”。

How do I fix this please help. 我该如何解决,请帮忙。

Looks like you have used Timer control on form designer and enabled it. 看起来您已经在窗体设计器上使用了Timer控件并启用了它。 This causes it to start ticking as soon as form is loaded. 这将导致它在加载表单后立即开始计时。 Make it false. 弄虚作假。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM