Please see my comments to the question. Your mistake is using the timer and trying to handle its events. To achieve what you need, you need to invert the processing: instead of timer events, handle keyboard events. Think about it: the real source of events is the keyboard events, as you want to time typing. You won't need any timer at all.
You actually have only three kinds of events: 1) start of timing; 2) keyboard events on each key when the user types those words; 3) end of timing. Use an instance of
System.Diagnostics.Stopwatch
, reset and start it on first event, stop in on third event:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.reset.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.start.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.restart.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.stop.aspx[
^].
The elapsed time will be your
Stopwatch.Elapsed
:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.elapsed.aspx[
^].
Now, you need to find how many words was typed during this time. The word is finished when a user types a blank space, tab or any punctuation character, including, say, end of line. If you want only the word containing only the character, test if the typed character is not letter (see below). The last word can be counted when timing has stopped (but it could be incomplete word, so taking it into account or not is a philosophical question :-)). Handle the event
KeyPress
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress%28v=vs.110%29.aspx[
^].
To classify typed characters in word or non-word, uses the methods of
System.Char
, such as:
http://msdn.microsoft.com/en-us/library/6w3ahtyy.aspx[
^],
http://msdn.microsoft.com/en-us/library/cta536cf.aspx[
^],
http://msdn.microsoft.com/en-us/library/yyxz6h5w.aspx[
^].
Divide number of counted words by elapsed time. :-)
—SA