Solved

countdown clock

Posted on 2010-11-10
14
663 Views
Last Modified: 2013-12-17

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();
        }
0
Comment
Question by:VBdotnet2005
  • 6
  • 6
  • 2
14 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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

0
 
LVL 10

Expert Comment

by:hosneylk
Comment Utility
and to stop it at 0..
void OnTimerTick(object sender, EventArgs args)

    {

        textBox1.Text = value--;

        if (value == 0) tmr.Stop();

    }

Open in new window

0
 
LVL 10

Expert Comment

by:hosneylk
Comment Utility
       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();
        }
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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  :)
0
 

Author Comment

by:VBdotnet2005
Comment Utility

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


      }
0
 
LVL 10

Expert Comment

by:hosneylk
Comment Utility
try this
private void Star_countdown()

        {

           countDown = 9;

            tmr.Interval = TimeSpan.FromSeconds(1);

            tmr.Tick += onTimerTick;

       

           

        }

Open in new window

0
 

Author Comment

by:VBdotnet2005
Comment Utility
not right still...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:VBdotnet2005
Comment Utility
it still does not reset it
0
 
LVL 10

Expert Comment

by:hosneylk
Comment Utility
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

0
 

Author Comment

by:VBdotnet2005
Comment Utility
it displays nothing
0
 
LVL 10

Expert Comment

by:hosneylk
Comment Utility
you need to put tmr.Start() within Star_countdown. And make sure you call it first.
0
 

Author Comment

by:VBdotnet2005
Comment Utility
I don't need to call Star_countdown inside btnSubmit? Still not working.
0
 
LVL 10

Accepted Solution

by:
hosneylk earned 125 total points
Comment Utility
I just tried this..
        int countDown = 9;

        DispatcherTimer tmr = new DispatcherTimer();



        public Form1()

        {

            InitializeComponent();

            

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

        }



        private void button1_Click(object sender, EventArgs e)

        {

            tmr.Stop();

            countDown = 9;

            tmr.Start();

        }

Open in new window

0
 

Author Comment

by:VBdotnet2005
Comment Utility
Yes, it works!!! must have been late at night kind of thing. Thank you very much
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now