crazy programmer
crazy programmer

Reputation: 33

how to add same timer in multiple window form in c#

how can i add same timer in multiple window form in c#.i am trying to develope an M.C.Q s app so when user answer first question if time is 25 second that time after clicking next how can i show the running time in next form.Thank you

Upvotes: 2

Views: 1127

Answers (3)

Tomas Chabada
Tomas Chabada

Reputation: 3019

If you want to share timer across different forms, you have two options.

1: pass timer instance between forms

var myTimer = new Timer(...);    
//here this means form1
this.MyTimer = myTimer; 
form2.MyTimer = myTimer;

2: make a timer instance static (and public) so you can access it from everywhere

public static Timer MyTimer = new Timer(...);

Upvotes: 0

Prasad Telkikar
Prasad Telkikar

Reputation: 16079

You can pass instance of timer as a parameter to next form constructor, so you can carry forward same time in next form.

Lets take an example:

You have first question on From1 and on Submit function you want to show next form something like this:

public class Form1
{
   ...
   public void submit()
   {
       ...
       //Here you are calling form2
       //Pass timer instance a parameter to form2
       Form2 form2Instance = new Form2(timer);
       form2Instance.Show();
   }

}

Upvotes: 1

PepitoSh
PepitoSh

Reputation: 1836

Put your timer as a static member in a form that inherits from Form. All other high level forms should inherit from the one with the timer.

Upvotes: 0

Related Questions