laurence keith albano
laurence keith albano

Reputation: 1482

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.

Upvotes: 0

Views: 332

Answers (1)

Nikhil Vartak
Nikhil Vartak

Reputation: 5117

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.

Upvotes: 2

Related Questions