VBdotnet2005
asked on
countdown clock
How can I create a countdown clock 9 to zero?
DispatcherTimer tmr = new DispatcherTimer();
tmr.Interval = TimeSpan.FromSeconds(1);
tmr.Tick += OnTimerTick;
tmr.Start();
}
void OnTimerTick(object sender, EventArgs args)
{
textBox1.Text = DateTime.Now.ToString();
}
and to stop it at 0..
void OnTimerTick(object sender, EventArgs args)
{
textBox1.Text = value--;
if (value == 0) tmr.Stop();
}
int countDown = 9;
public Form1()
{
InitializeComponent();
DispatcherTimer tmr = new DispatcherTimer();
tmr.Interval = TimeSpan.FromSeconds(1);
tmr.Tick += OnTimerTick;
tmr.Start();
}
void OnTimerTick(object sender, EventArgs args)
{
textBox1.Text = countDown--.ToString();
if (countDown < 0) ((DispatcherTimer)sender). Stop();
}
public Form1()
{
InitializeComponent();
DispatcherTimer tmr = new DispatcherTimer();
tmr.Interval = TimeSpan.FromSeconds(1);
tmr.Tick += OnTimerTick;
tmr.Start();
}
void OnTimerTick(object sender, EventArgs args)
{
textBox1.Text = countDown--.ToString();
if (countDown < 0) ((DispatcherTimer)sender).
}
lol... I even watched the darn thing print "-1" and I told myself, "I need to put a stop condition in there..." I must be tired :)
ASKER
When click the second time, textbox4.txt time does not reset.
I tried tmr.stop & tmr.start, it went to - 2 and so on.
int countdown = 9;
private void Star_countdown()
{
tmr.Interval = TimeSpan.FromSeconds(1);
tmr.Tick += onTimerTick;
}
void onTimerTick(object sender, EventArgs args)
{
textBlock4.Text = countdown--.ToString();
if (countdown < 0) ((DispatcherTimer)sender).
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
star_countdown();
tmr.start();
}
try this
private void Star_countdown()
{
countDown = 9;
tmr.Interval = TimeSpan.FromSeconds(1);
tmr.Tick += onTimerTick;
}
ASKER
not right still...
ASKER
it still does not reset it
call Star_countdown() only once (like in form initialize)
int countdown = 9;
private void Star_countdown()
{
tmr.Interval = TimeSpan.FromSeconds(1);
tmr.Tick += onTimerTick;
}
void onTimerTick(object sender, EventArgs args)
{
textBlock4.Text = countdown--.ToString();
if (countdown < 0) ((DispatcherTimer)sender).Stop();
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
tmr.Stop();
countDown = 9;
tmr.Start();
}
ASKER
it displays nothing
you need to put tmr.Start() within Star_countdown. And make sure you call it first.
ASKER
I don't need to call Star_countdown inside btnSubmit? Still not working.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Yes, it works!!! must have been late at night kind of thing. Thank you very much
Open in new window