Control timer in form 1 from form 2, C#

user1436685 picture user1436685 · Jul 6, 2012 · Viewed 7.3k times · Source

A label in form is displaying the count of timer. Now i want to stop,start and reset it using form 2. How can i do this.plz help

Answer

JMK picture JMK · Jul 6, 2012

Forms are just classes, and the timer on Form 2 is an object inside that class.

You can change the Modifiers property of your timer to public, and then instantiate Form 2 inside Form 1, call the Show() method of Form 2, and then access your timer object which is now public.

So you have a project with 2 forms like so:

Solution Explorer

Create a button in Form 1 like so:

Form One

Place a timer object on Form 2 and change the access modifier like so:

Properties

Then put the following code under your button in form one:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.Show();
    f2.timer1.Enabled = true;
}

Now you can launch form 2 and access all of the properties on the timer on form 2 from form 1.

Does this help?