I have Form with a Timer1, and it set to 10Sec.
There is a KeyDown event – when the user press “Enter”, I would like to save in “ans” the time duration that past in the 10S interval before it ends.
For Example: If i starting the timer1 now, and after 3Sec, I’m pressing Enter, ans = 3. and if I didnt press any key, ans will be equal to 10.
I have this code:
if (e.KeyCode == Keys.Enter)
{
ResponseTimeList.Add(timer1.Interval);
}
*ResponseTimeList is:
public List<double> ResponseTimeList = new List<double>();
How can i improve it?
Thanks.
Well, to start out with, Timer is not what you want to use. The timer class is designed to fire off events at a pre-defined interval of time; for example, you might use a timer to update a text box on a form every 10 seconds.
Instead, what you wish to do is use a stopwatch (System.Diagnostics.Stopwatch). Call Stopwatch.Start() whenever you want to start timing. When the user presses enter, simply call Stopwatch.Stop() and then get the time interval that has elapsed in seconds.
Finally, for the 10-second logic, you will need to use something like this (a conditional evaluation):