Valerie Meunier

Valerie Meunier

  • 954
  • 693
  • 72.5k

how to start automatically the timer?

Dec 29 2021 12:59 PM

Hi

i want to start automatically the timer, but nothing happens. According to me, it should because timer1.Enabled = true; I also tried with timer1.Start().

Thanks

public partial class Form1 : Form
    {
        int timerest = 5;
        private void Form1_Load(object sender, EventArgs e)
        {
            Timer timer1 = new Timer();
            timer1.Interval = 1000;//=1 sec
            timer1.Enabled = true;
         }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timerest > 0)
            {
                Console.Beep();
                listBox1.Items.Add(DateTime.Now.ToLongTimeString() + "," + DateTime.Now.ToLongDateString());
            }
            else if (timerest == 0)
                listBox1.Items.Add("Form will close.");
            else if (timerest == -2)
                this.Close();
            timerest--;

        }
    }
 


Answers (1)