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

        }

我的計時器有問題,當我啟動程序時,計時器將自動倒計時,而無需按按鈕;如果單擊按鈕,計時器將遞減1。

示例:當我單擊按鈕時,“ 56剩余”將減少計時器,結果將為“ 54剩余”,依此類推。 (單擊按鈕)從“ 54至51”。

我該如何解決,請幫忙。

看起來您已經在窗體設計器上使用了Timer控件並啟用了它。 這將導致它在加載表單后立即開始計時。 弄虛作假。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM