Windows Develop Bookmark and Share   
 index > Windows Forms General > help to access timer control
 

help to access timer control

hi,

i have a timer that gets started when the form loads( i used timer1.start() method to do this) . i have button which when clicked should display the time it took from the time the timer was started until the button was clicked ..how do i do this in visual c# ?

Apart from that , i want the timer to reset and start again when the action is performed...

Waiting for your reply.

-Sweety

sweet_salt  Sunday, January 07, 2007 7:18 PM

You should use the Stopwatch for this:

There is even an example in the documenation that answers your question.

Gabriel Lozano-Morán  Sunday, January 07, 2007 7:36 PM

well you could have a global variable which holds a value, such as say a DateTime object - this datetime object will have seconds added it to it in the timer tick event (set the timer interval to 1000 ms for 1 second). Then when the button has been pressed, stop the timer and obtain the value from the global datetime object which is used to hold the seconds. Example:

private DateTime totalTime = new DateTime();

..

..

private void timer1_Tick(object sender, EventArgs e)

{

this.totalTime = this.totalTime.AddSeconds(1);

}

private void button1_Click(object sender, EventArgs e)

{

this.timer1.Stop();

MessageBox.Show(this.totalTime.ToString());

}

and thats it!

ahmedilyas  Sunday, January 07, 2007 7:38 PM

You should use the Stopwatch for this:

There is even an example in the documenation that answers your question.

Gabriel Lozano-Morán  Sunday, January 07, 2007 7:36 PM

well you could have a global variable which holds a value, such as say a DateTime object - this datetime object will have seconds added it to it in the timer tick event (set the timer interval to 1000 ms for 1 second). Then when the button has been pressed, stop the timer and obtain the value from the global datetime object which is used to hold the seconds. Example:

private DateTime totalTime = new DateTime();

..

..

private void timer1_Tick(object sender, EventArgs e)

{

this.totalTime = this.totalTime.AddSeconds(1);

}

private void button1_Click(object sender, EventArgs e)

{

this.timer1.Stop();

MessageBox.Show(this.totalTime.ToString());

}

and thats it!

ahmedilyas  Sunday, January 07, 2007 7:38 PM

You can use google to search for other answers

Custom Search

More Threads

• remap the menu key to a cntrl key vista 32bit
• My own icon
• Form1_Load or OnLoad?
• Reading code from a file.
• A problem in TreeView.OnBeforeLabelEdit event
• Parameters
• checkedlistbox
• Installing a windows service with installUtil.exe
• webbrowser - ShowSaveAsDialog fails to save web page
• DateTimePicker