Hello Everyone,

I have a timer that starts when a button is clicked on Form1. When the time elapses Form2 appears. On Form2 I want to stop the timer and restart it from a button on Form2 however the timer does not stop.

Here is my code for Form1:

public Form1()
        {
            InitializeComponent();
        }

        public Form2 frm2;
        private void start_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();
            timer1.Tick += new EventHandler(timer1_Tick);
        }

        public void timer1_Tick(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
        }

Here is the code for Form2

public Form2()
        {
            InitializeComponent();
        }
        public Form1 frm1;
        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void restart_Click(object sender, EventArgs e)
        {
            frm1 = new Form1();
            frm1.timer1.Enabled = false;
         }

I would appreciate any help with this. (I can stop the timer is the buttons are on the same Form)

Thanks in advance,

SubProf

Hi

A cleaner way would be to pass a reference of Form1 to Form2 in its constructor.
Then you could make a public method on Form1 which Form2 can access and control the timer.

public void EnableTimer()
        {
            timer1.Enabled = false;
         }

Regards
:)

Hi Mick,

That solved my problem - thanks

SubProf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.