0

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

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by SubProf
0

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
:)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.