Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1 minute countdown timer

I am trying to do a simple countdown timer from 1 minute, that is shown in the button text. When the button is pressed I just want it to count down to 0, the show "times up". I have been reading all the posts I can find trying to figure out how to do this and cannot. Can someone tell me what I am doing wrong.

This is in in visual c# windows phone application. I hope I made the post appear correctly, this is my first time to ask a question on this site, I am new to this. Thank you in advance for any advice.

void bTime_Click(object sender, RoutedEventArgs e)
    {
        DispatcherTimer timer1 = new DispatcherTimer();
        timer1.Interval = TimeSpan.FromSeconds(60);
        timer1.Tick += new EventHandler(timer_Tick);
        timer1.Start();
    }

    int tik = 60;
    void timer_Tick(object sender, EventArgs e)
    {
        bTime.Content = timer.ToString();
        if (tik > 0)
            Countdown.Text = (timer--).ToString();
        else
            Countdown.Text = "Times Up";
        throw new NotImplementedException();
    }
like image 550
Patrick W. Avatar asked Dec 20 '25 11:12

Patrick W.


2 Answers

First, get rid of throw new NotImplementedException. Second, you need to decrement tik. So something like this:

    DispatcherTimer timer1 = new DispatcherTimer();
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        timer1.Interval = new TimeSpan(0, 0, 0, 1);
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Start();
    }

    int tik = 60;
    void timer1_Tick(object sender, EventArgs e)
    {
        Countdown.Text = tik + " Seconds Remaining";
        if (tik > 0)
            tik--;
        else
            Countdown.Text = "Times Up";
    }

I have changed Interval and i'm decrementing tik every second. Nice and simple. Hope it helps. Let me know if you don't understand.

like image 158
Subby Avatar answered Dec 22 '25 20:12

Subby


What is wrong with your code? To me it looks like these parts:

bTime.Content = timer.ToString();

and

bTime.Content = timer.ToString();

First of all, I don't even know what the variable timer is. Is it supposed to be timer1?

tik is never getting subtracted from and will always stay at 60.

Why don't you change your code to this:

DispatcherTimer timer1 = new DispatcherTimer();
void bTime_Click(object sender, RoutedEventArgs e)
{
    timer1.Interval = TimeSpan.FromSeconds(60);
    timer1.Tick += new EventHandler(timer_Tick);
    timer1.Start();
}

int tik = 60;
void timer_Tick(object sender, EventArgs e)
{
    bTime.Content = tik.ToString();
    if (tik > 0)
        Countdown.Text = (tik--).ToString();
    else
    {
        Countdown.Text = "Times Up";
        timer1.Stop();
    }
    throw new NotImplementedException();
}
like image 30
SuperPrograman Avatar answered Dec 22 '25 20:12

SuperPrograman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!