2

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

7
  • 1
    you can use a global timer for whole project which can be in a static class or you can use MDI winforms .1st one is simpler than 2nd if you're a beginner Commented Jun 12, 2018 at 6:41
  • 1
    May not contribute to your question but: Lets say you got 25 questions, do you have 25 Forms then? Commented Jun 12, 2018 at 6:41
  • let i've added 10 question in my app and total time is 3 minutes.Timer starts from 0 and when user answer the first one and click next then i want to know that how can the timer show the final time of question one as initial time of question two Commented Jun 12, 2018 at 6:44
  • I would suggest to manage the program from module Program.cs and then launch forms from that parent module. Then, you can have, as suggested a timer in a Global static class Commented Jun 12, 2018 at 6:45
  • Do you mean a timer or do you what the time elapsed to be displayed? Commented Jun 12, 2018 at 7:19

3 Answers 3

1

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();
   }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Prasad can you post an example of that one!
0

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.

Comments

0

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(...);

2 Comments

kindly post an example of hat one cuz im begginer
And the 3rd option that is the most OOP-spirited the one I suggested.:-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.