Link to home
Create AccountLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

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();
        }
Avatar of kaufmed
kaufmed
Flag of United States of America image

Set your DispatchTimer to be class-level. Then add a private variable to hold the value you want to display. In the Tick event, display and decrement the value.
public partial class Form1 : Form
{
    private DispatcherTimer tmr;
    private int value;

    public Form1()
    {
        InitializeComponent();

        this.tmr = new DispatcherTimer();
        this.value = 9;
        this.tmr.Interval = TimeSpan.FromSeconds(1);
        this.tmr.Tick += OnTimerTick;
        this.tmr.Start();
    }

    void OnTimerTick(object sender, EventArgs args)
    {
        textBox1.Text = this.value.ToString();
        this.value--;
    }
}

Open in new window

and to stop it at 0..
void OnTimerTick(object sender, EventArgs args)
    {
        textBox1.Text = value--;
        if (value == 0) tmr.Stop();
    }

Open in new window

       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();
        }
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  :)
Avatar of VBdotnet2005

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).Stop();

        }
       
        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;
       
           
        }

Open in new window

not right still...
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();


      }

Open in new window

it displays nothing
you need to put tmr.Start() within Star_countdown. And make sure you call it first.
I don't need to call Star_countdown inside btnSubmit? Still not working.
ASKER CERTIFIED SOLUTION
Avatar of hosneylk
hosneylk
Flag of Singapore image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Yes, it works!!! must have been late at night kind of thing. Thank you very much