Solved

countdown clock

Posted on 2010-11-10
14
672 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 75

Expert Comment

by:käµfm³d 👽
ID: 34108056
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
ID: 34108074
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
ID: 34108102
       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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34108113
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
ID: 34109282

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
ID: 34109299
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
ID: 34109359
not right still...
0
 

Author Comment

by:VBdotnet2005
ID: 34109364
it still does not reset it
0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34109370
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
ID: 34109416
it displays nothing
0
 
LVL 10

Expert Comment

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

Author Comment

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

Accepted Solution

by:
hosneylk earned 125 total points
ID: 34109455
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
ID: 34113028
Yes, it works!!! must have been late at night kind of thing. Thank you very much
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 33
Amazon S3 .Net error 5 21
Why use this lambda? 12 39
Upgrade code from VS 2010 to VS 2015 7 20
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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