简体   繁体   中英

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.

Example: "56 Remaining" when I clicked the button the timer will decrement and the result will be "54 Remaining" and so on. (Button is clicked) from "54 to 51".

How do I fix this please help.

Looks like you have used Timer control on form designer and enabled it. This causes it to start ticking as soon as form is loaded. Make it false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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