none
Using multiple count down timers in Visual Studio 2015 working independently C# RRS feed

  • Question

  • Hi, I am trying to get 3 or more times counting down the value of countdown are set in textboxes.

    when I start timer 1 it works but as soon as I start timer 2 or 3 the time restarts on time1 and also the same thing happens with any of the timers once the first one starts. From what I have read I need to get them to work independently from each timer.

    3x timers (timer1, timer2,timer3)

    9x label 

    9x textbox

    or is there an easier way to do this, any help would be appreciated.

    the code is below  (c# visual studio)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class <g class="gr_ gr_616 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="616" id="616">Form1 :</g> Form
        { int h; int m; int s;
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text == "")
                {
                    textBox1.Text = "0";
                }
                if (textBox2.Text == "")
                {
                    textBox2.Text = "0";
                }
                if (textBox3.Text == "")
                {
                    textBox3.Text = "0";
                }
                h = Convert.ToInt32(textBox1.Text);
                m = Convert.ToInt32(textBox2.Text);
                s = Convert.ToInt32(textBox3.Text);
                timer1.Start();
            }

            private void button3_Click(object sender, EventArgs e)
            {
                if (textBox4.Text == "")
                {
                    textBox4.Text = "0";
                }
                if (textBox5.Text == "")
                {
                    textBox5.Text = "0";
                }
                if (textBox6.Text == "")
                {
                    textBox6.Text = "0";
                }
                h = Convert.ToInt32(textBox4.Text);
                m = Convert.ToInt32(textBox5.Text);
                s = Convert.ToInt32(textBox6.Text);
                timer2.Start();
            }

            private void button5_Click(object sender, EventArgs e)
            {
                if (textBox7.Text == "")
                {
                    textBox7.Text = "0";
                }
                if (textBox8.Text == "")
                {
                    textBox8.Text = "0";
                }
                if (textBox9.Text == "")
                {
                    textBox9.Text = "0";
                }
                h = Convert.ToInt32(textBox7.Text);
                m = Convert.ToInt32(textBox8.Text);
                s = Convert.ToInt32(textBox9.Text);
                timer3.Start();
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                s = s - 1;
                if (s == -1)
                {
                    m = m - 1;
                    s = 59;
                }
                if (m == -1)
                {
                    h = h - 1;
                    m = 59;
                }
                if (h == 0 && m == 0 && s == 0)
                {
                    timer1.Stop();
                    MessageBox.Show("Times up!", "Time");
                }
                string hh = Convert.ToString(h);
                string mm = Convert.ToString(m);
                string ss = Convert.ToString(s);
                label1.Text = hh;
                label2.Text = mm;
                label3.Text = ss;
            }

            private void timer2_Tick(object sender, EventArgs e)
            {
                s = s - 1;
                if (s == -1)
                {
                    m = m - 1;
                    s = 59;
                }
                if (m == -1)
                {
                    h = h - 1;
                    m = 59;
                }
                if (h == 0 && m == 0 && s == 0)
                {
                    timer2.Stop();
                    MessageBox.Show("Times up!", "Time");
                }
                string hh = Convert.ToString(h);
                string mm = Convert.ToString(m);
                string ss = Convert.ToString(s);
                label4.Text = hh;
                label5.Text = mm;
                label6.Text = ss;
            }

            private void timer3_Tick(object sender, EventArgs e)
            {
                s = s - 1;
                if (s == -1)
                {
                    m = m - 1;
                    s = 59;
                }
                if (m == -1)
                {
                    h = h - 1;
                    m = 59;
                }
                if (h == 0 && m == 0 && s == 0)
                {
                    timer3.Stop();
                    MessageBox.Show("Times up!", "Time");
                }
                string hh = Convert.ToString(h);
                string mm = Convert.ToString(m);
                string ss = Convert.ToString(s);
                label7.Text = hh;
                label8.Text = mm;
                label9.Text = ss;
            }

            private void button2_Click(object sender, EventArgs e)
            {
                timer1.Stop();
            }

            private void button4_Click(object sender, EventArgs e)
            {
                timer2.Stop();
            }

            private void button6_Click(object sender, EventArgs e)
            {
                timer3.Stop();
            }
        }

    • Moved by 宝宝徐 Wednesday, December 27, 2017 6:45 AM
    Wednesday, December 27, 2017 2:43 AM

All replies

  • I have not looked really close at your code but I see that timer1_Tick, timer2_Tick and timer3_Tick use whatever value is in s, m and h. Is that intentional?


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, December 27, 2017 4:47 AM
  • Hi,

    I will move your thread to c# forum for professional support, if you have problems about visual studio IDE, please feel free to let me know.

    Best regards,

    Joyce


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 27, 2017 6:43 AM
  • Hi Sam 

    That was it thanks for your help

    Cheers


    • Edited by vautek Wednesday, December 27, 2017 6:48 AM
    Wednesday, December 27, 2017 6:47 AM
  • Hi vautek,

    Please try the code below. The Timers work independently.

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            int h1, m1, s1;
            int h2, m2, s2;
            int h3, m3, s3;
            private void button1_Click(object sender, EventArgs e)
            {
    
                if (textBox1.Text == "")
                {
                    textBox1.Text = "0";
                }
                if (textBox2.Text == "")
                {
                    textBox2.Text = "0";
                }
                if (textBox3.Text == "")
                {
                    textBox3.Text = "0";
                }
                h1 = Convert.ToInt32(textBox1.Text);
                m1 = Convert.ToInt32(textBox2.Text);
                s1 = Convert.ToInt32(textBox3.Text);
                timer1.Start();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                timer1.Stop();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (textBox4.Text == "")
                {
                    textBox4.Text = "0";
                }
                if (textBox5.Text == "")
                {
                    textBox5.Text = "0";
                }
                if (textBox6.Text == "")
                {
                    textBox6.Text = "0";
                }
                h2 = Convert.ToInt32(textBox4.Text);
                m2 = Convert.ToInt32(textBox5.Text);
                s2 = Convert.ToInt32(textBox6.Text);
                timer2.Start();
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                s1 = s1 - 1;
                if (s1 == -1)
                {
                    m1 = m1 - 1;
                    s1 = 59;
                }
                if (m1 == -1)
                {
                    h1 = h1 - 1;
                    m1 = 59;
                }
                if (h1 == 0 && m1 == 0 && s1 == 0)
                {
                    timer1.Stop();
                    MessageBox.Show("Times up!", "Time");
                }
                string hh = Convert.ToString(h1);
                string mm = Convert.ToString(m1);
                string ss = Convert.ToString(s1);
                label1.Text = hh;
                label2.Text = mm;
                label3.Text = ss;
            }
    
            private void timer2_Tick(object sender, EventArgs e)
            {
                s2 = s2 - 1;
                if (s2 == -1)
                {
                    m2 = m2 - 1;
                    s2 = 59;
                }
                if (m2 == -1)
                {
                    h2 = h2 - 1;
                    m2 = 59;
                }
                if (h2 == 0 && m2 == 0 && s2 == 0)
                {
                    timer2.Stop();
                    MessageBox.Show("Times up!", "Time");
                }
                string hh = Convert.ToString(h2);
                string mm = Convert.ToString(m2);
                string ss = Convert.ToString(s2);
                label4.Text = hh;
                label5.Text = mm;
                label6.Text = ss;
            }
    
            private void timer3_Tick(object sender, EventArgs e)
            {
                s3 = s3 - 1;
                if (s3 == -1)
                {
                    m3 = m3 - 1;
                    s3 = 59;
                }
                if (m3 == -1)
                {
                    h3 = h3 - 1;
                    m3 = 59;
                }
                if (h3 == 0 && m3 == 0 && s3 == 0)
                {
                    timer3.Stop();
                    MessageBox.Show("Times up!", "Time");
                }
                string hh = Convert.ToString(h3);
                string mm = Convert.ToString(m3);
                string ss = Convert.ToString(s3);
                label7.Text = hh;
                label8.Text = mm;
                label9.Text = ss;
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                if (textBox7.Text == "")
                {
                    textBox7.Text = "0";
                }
                if (textBox8.Text == "")
                {
                    textBox8.Text = "0";
                }
                if (textBox9.Text == "")
                {
                    textBox9.Text = "0";
                }
                h3 = Convert.ToInt32(textBox7.Text);
                m3 = Convert.ToInt32(textBox8.Text);
                s3 = Convert.ToInt32(textBox9.Text);
                timer3.Start();
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                timer2.Stop();
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                timer3.Stop();
            }
        }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 28, 2017 7:26 AM
    Moderator