none
countdown timer problem RRS feed

  • Question

  • hi guys

    i make countdown timers , after 10 secounds int count * 2 and again reset countdown timers.

    i want 5 time per 10 sec count * 2 and in time 5 stop timer.

      private DateTime startTime;
            private void Form1_Load(object sender, EventArgs e)
            {
                label1.Text = TimeSpan.FromSeconds(10).ToString("dd\\:hh\\:mm\\:ss");
                timer1.Interval = 1000;
            }
    
            public void counter()
            {
                int count = 300;
                count = count * 2;
                label2.Text = count + " $";
                label1.Text = (TimeSpan.FromSeconds(10) - (DateTime.Now - startTime)).ToString("dd\\:hh\\:mm\\:ss");
                startTime = DateTime.Now;
                timer1.Start();
    
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                if(timer1.Interval <= 0)
                {
                    counter();
                }
                else
                {
                    label1.Text = (TimeSpan.FromSeconds(10) - (DateTime.Now - startTime)).ToString("dd\\:hh\\:mm\\:ss");
                }
             
           
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                startTime = DateTime.Now;
                timer1.Start();
            }

    Thursday, January 3, 2019 6:41 PM

All replies

  • Your description is a little confusing.  Are you saying you want 10 seconds, then "count" doubles, then 10 seconds, then count doubles, then 10 seconds, then count doubles, and after the 5th time, stop the counting?

    The first problem is that "count" must be a class member variable.  The way you have it now, "count" will reset to 300 every time counter fires.  The second problem is that the timer Interval is never going to be less than 0.  If you want to update the time every second but take other action every 10 seconds, then you have to do the "10 to 0" countdown yourself.

        private int countdown;
        private int countdown;
        private DateTime startTime;
    
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = TimeSpan.FromSeconds(10).ToString("dd\\:hh\\:mm\\:ss");
            timer1.Interval = 1000;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            countdown = 50;
            counter = 300;
            startTime = DateTime.Now;
            timer1.Start();
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            if( (--countdown % 10) == 0 )
            {
                count *= 2;
                label2.Text = count + " $";
            }
    
            label1.Text = TimeSpan.FromSeconds(countdown%10)).ToString("00:ss");
    
            if( countdown == 0 )
            {
                timer1.Stop();
            }
        }
    
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, January 4, 2019 2:08 AM
  • Your description is a little confusing.  Are you saying you want 10 seconds, then "count" doubles, then 10 seconds, then count doubles, then 10 seconds, then count doubles, and after the 5th time, stop the counting?

    The first problem is that "count" must be a class member variable.  The way you have it now, "count" will reset to 300 every time counter fires.  The second problem is that the timer Interval is never going to be less than 0.  If you want to update the time every second but take other action every 10 seconds, then you have to do the "10 to 0" countdown yourself.

        private int countdown;
        private int countdown;
        private DateTime startTime;
    
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = TimeSpan.FromSeconds(10).ToString("dd\\:hh\\:mm\\:ss");
            timer1.Interval = 1000;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            countdown = 50;
            counter = 300;
            startTime = DateTime.Now;
            timer1.Start();
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            if( (--countdown % 10) == 0 )
            {
                count *= 2;
                label2.Text = count + " $";
            }
    
            label1.Text = TimeSpan.FromSeconds(countdown%10)).ToString("00:ss");
    
            if( countdown == 0 )
            {
                timer1.Stop();
            }
        }
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    sure

    but problem for:

      label1.Text = TimeSpan.FromSeconds(countdown % 10)).ToString("00:ss");

    can't convert TimeSpan to string


    Friday, January 4, 2019 6:11 AM

  • but problem for:

      label1.Text = TimeSpan.FromSeconds(countdown % 10)).ToString("00:ss");

    can't convert TimeSpan to string


    What exactly are you trying to do with that format string?

    "00:ss" is not valid.

    If you try it *without* that format string, does it run without errors?

    Note also that you have one too many right round brackets. Try:

    label1.Text = TimeSpan.FromSeconds(countdown % 10).ToString();

    - Wayne

    Friday, January 4, 2019 9:25 AM
  • Also try this:

    label1.Text = "00:" + TimeSpan.FromSeconds(countdown % 10).ToString("ss");
    
    

    - Wayne

    Friday, January 4, 2019 9:56 AM
  • Well, you should have been able to figure that out.  ("\\0\\0\\:ss") would work, but since it's a simple integer, you could also just use String.Format("00:{0:D2}", countdown%10);.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Saturday, January 5, 2019 12:57 AM