1 minute countdown timer
Solution 1
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.
Solution 2
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();
}
Comments
-
Patrick W. almost 2 years
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(); }
-
Patrick W. almost 12 yearsThis worked perfect, now that I see it it makes more sense. Thank you!