-2

How do I stop my timer when it reaches zero? I have this windows form code here:

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
    {
        int minutes = 2;
        int seconds = 0;
        private TimeSpan countdowntime;

        public MainGame()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        private void NewGame_Click(object sender, EventArgs e)
        {
            setcountdown(minutes, seconds);
            timer1.Enabled = true;
            timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan tSecond = new TimeSpan(0, 0, 1);
            countdowntime = countdowntime.Subtract(tSecond);


            if(seconds > 0)
            {
                timeremaining.Text = countdowntime.Minutes.ToString() + ":" + countdowntime.Seconds.ToString();

            }
            else
            {
                timeremaining.Text = "Times Up!";
                timer1.Stop();
            }
        }
        private void setcountdown(int min,int sec)
        {
            TimeSpan temp = new TimeSpan(0 , min , sec);
            countdowntime = temp;
        }

The code works but once it reaches "0:0" , it will continue to decrement to "0:-1" and so on, my condition doesn't seem to work when stopping the timer.

This is my condition from the above:

        if(seconds > 0)
        {
            timeremaining.Text = countdowntime.Minutes.ToString() + ":" + countdowntime.Seconds.ToString();

        }
        else
        {
            timeremaining.Text = "Times Up!";
            timer1.Stop();
        }
Mainak
  • 469
  • 3
  • 9
  • 33
laurence keith albano
  • 1,409
  • 4
  • 27
  • 59
  • 2
    You're initializing your class-scope `seconds` field but never assign a value to it afterwards. This means that your `if` statement will always evaluate to the same thing and never hit the condition. I think you forgot to write a line of code where `countdowntime` is assigned to `seconds` or something like that. Though that assignment seems unnecessary and you should just be able to evaluate `countdowntime` in your `if` statement. – Paul Sasik Sep 23 '15 at 04:16
  • Could you have a sample code here on how to implement it? I quite new to this windows form project and timers also. – laurence keith albano Sep 23 '15 at 04:20
  • Change `if(seconds > 0)` to `if (countdowntime.TotalSeconds > 0)`. You are evaluating if there is any second left in the countdowntime. your `seconds` was never decremented. – Viet Nguyen Sep 23 '15 at 04:23
  • That condition is working sir, thank you. But in the time "0:1" after 1sec it will display "Times Up!" how do I change it when the timer reaches "0:0" the text display "Times Up!". – laurence keith albano Sep 23 '15 at 04:34
  • 1
    That is one more loop you need, so change to `if (countdowntime.TotalSeconds >= 0)`. Btw, if you find the answer useful, you could upvote. Thanks. – Viet Nguyen Sep 23 '15 at 04:45
  • I've tried that sir, it works but I want to display "Times Up" when the timer reaches "0:0". My Code doesn't display "Times Up" it will only display "0:0" once it reaches there. My Condition: if(countdowntime.TotalSeconds >= 0) { timeremaining.Text = countdowntime.Minutes.ToString() + ":" + countdowntime.Seconds.ToString(); } else { timeremaining.Text = "Times Up"; } – laurence keith albano Sep 23 '15 at 04:57

2 Answers2

1

Following my comments. You could change your timer code to this. It will start with displaying your initial value

private void timer1_Tick(object sender, EventArgs e)
{
    //I've edited you're code if(countdowntime.TotalSeconds > 0)
    //to this because @ "0:1" It will display Times Up
    if (countdowntime.TotalSeconds >= 0)
    {
        timeremaining.Text = countdowntime.Minutes.ToString() + ":" + countdowntime.Seconds.ToString();
    }
    else
    {
        timeremaining.Text = "Times Up!\r\n";
        timer1.Stop();
        return;
    }

    TimeSpan tSecond = new TimeSpan(0, 0, 1);
    countdowntime = countdowntime.Subtract(tSecond);
}
laurence keith albano
  • 1,409
  • 4
  • 27
  • 59
Viet Nguyen
  • 406
  • 3
  • 12
0

I am not sure this will work, but I think it would:

private void timer1_Tick_1(object sender, EventArgs e)
{
   counter--; [COLOR=#006400]//Decrease the counter, just like the timer decreases.[/COLOR]
   if (counter == 0) [COLOR=#006400]//If we hit 0, or any other number we'd like to check.[/COLOR]
    {
      MessageBox.Show("Time Up!");
      counter = timer.Interval;[COLOR=#006400]//Reset the counter.[/COLOR]
      timer.Start();[COLOR=#006400]//Re-start the timer.[/COLOR]
    }        
}

DEMO Page

Stop Number

Community
  • 1
  • 1
Ivin Raj
  • 3,448
  • 2
  • 28
  • 65