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
-
1you 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 beginnerLucifer– Lucifer2018-06-12 06:41:24 +00:00Commented Jun 12, 2018 at 6:41
-
1May not contribute to your question but: Lets say you got 25 questions, do you have 25 Forms then?Cataklysim– Cataklysim2018-06-12 06:41:26 +00:00Commented 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 twocrazy programmer– crazy programmer2018-06-12 06:44:46 +00:00Commented 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 classCarles– Carles2018-06-12 06:45:25 +00:00Commented Jun 12, 2018 at 6:45
-
Do you mean a timer or do you what the time elapsed to be displayed?Enigmativity– Enigmativity2018-06-12 07:19:57 +00:00Commented Jun 12, 2018 at 7:19
|
Show 2 more comments
3 Answers
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();
}
}
1 Comment
crazy programmer
Prasad can you post an example of that one!
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
crazy programmer
kindly post an example of hat one cuz im begginer
PepitoSh
And the 3rd option that is the most OOP-spirited the one I suggested.:-)