|
-
Feb 8th, 2010, 08:44 AM
#1
Thread Starter
Member
Capturing a timers time?
The below is my program so far
Code:
Public Class Form1
Private Display As Boolean
Private WithEvents tmr As Windows.Forms.Timer
Private NewChar As Char
Private fnt As New Font(Me.Font.FontFamily, 72, FontStyle.Bold, GraphicsUnit.Point)
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
tmr = New Windows.Forms.Timer
tmr.Interval = 5000 ' 5 seconds = 5000 milliseconds
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Display = False
Me.Invalidate()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
tmr.Enabled = True
End Sub
'What happens after 5000 ms
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles tmr.Tick
' Make static so that the random is not seeded every time
Static r As New Random
' Get a random number between A(65) and Z(90).
NewChar = Chr(r.Next(65, 90))
' Make sure screen gets repainted
Me.Invalidate()
' Stop the timer so that it doesn't fetch the next character automatically
tmr.Stop()
End Sub
' Stopping and restarting timer
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (Char.ToUpper(e.KeyChar) = NewChar) Then
'Start the timer so that the next character is shown in 5 seconds
NewChar = ControlChars.NullChar ' < --------------------------
Me.Invalidate()
tmr.Start()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Start the timer so that the next character is shown in 5 seconds
tmr.Start()
Display = True
Me.Invalidate()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Display Then Dim textSize As SizeF = e.Graphics.MeasureString(NewChar, fnt)
If NewChar = ControlChars.NullChar Then Exit Sub
e.Graphics.DrawString(NewChar, fnt, Brushes.Red, x:=50, y:=50)
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
Upon clicking the button a series of letters are displayed to the screen.
I would like to be able to capture the time taken to press the letter shown.
Is there any way of doing this?
E.g capturing the results in a textbox?
-
Feb 8th, 2010, 08:58 AM
#2
Re: Capturing a timers time?
On the class level declare these variables:
Code:
Private TimerStart As DateTime
Private TimePassed As TimeSpan
When you invoke tmr.Start, immediately save the current time to the variable:
Whenever you want to measure the time passed from the TimerStart time just subtract the TimerStart from current time:"
Code:
TimePassed = Now - TimerStart
Textbox1.Text = TimePassed.ToString
-
Feb 8th, 2010, 09:47 AM
#3
Re: Capturing a timers time?
Use a stopwatch.
Code:
Dim stpw As New Stopwatch
Private Sub displayLetterCode()
'
'your code to display letter
'
stpw.Reset()
stpw.Start()
End Sub
Private Sub LetterWasPressedCode()
'
'
'
stpw.Stop()
'stpw.Elapsed is a TimeSpan that will have how much time Elapsed
End Sub
-
Feb 8th, 2010, 10:15 AM
#4
Thread Starter
Member
Re: Capturing a timers time?
Using the code below seems to add the time for the period between the two letters - I'm trying to just get the time from the letter being shown to the letter being pressed.
Whereabouts should I be putting the code?
Thanks
Code:
Public Class Form1
Private Display As Boolean
Private WithEvents tmr As Windows.Forms.Timer
Private NewChar As Char
Private fnt As New Font(Me.Font.FontFamily, 72, FontStyle.Bold, GraphicsUnit.Point)
Dim MyRandom As New Random
Private TimerStart As DateTime
Private TimePassed As TimeSpan
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
tmr = New Windows.Forms.Timer
tmr.Interval = 10000
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Display = False
Me.Invalidate()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
tmr.Enabled = True
End Sub
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles tmr.Tick
Static r As New Random
'Get a random time between 7 and 20 seconds for the timer
tmr.Interval = MyRandom.Next(7, 21) * 1000
' Get a random number between A(65) and Z(90).
NewChar = Chr(r.Next(65, 91))
' Make sure screen gets repainted
Me.Invalidate()
' Stop the timer so that it doesn't fetch the next character automatically
tmr.Stop()
TimePassed = Now - TimerStart
txtRecord.Text = TimePassed.ToString
End Sub
' Stopping and restarting timer
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (Char.ToUpper(e.KeyChar) = NewChar) Then
NewChar = ControlChars.NullChar
Me.Invalidate()
tmr.Start()
TimerStart = Now
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tmr.Start()
TimerStart = Now
Display = True
Me.Invalidate()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Display Then Dim textSize As SizeF = e.Graphics.MeasureString(NewChar, fnt)
If NewChar = ControlChars.NullChar Then Exit Sub
e.Graphics.DrawString(NewChar, fnt, Brushes.Red, x:=50, y:=50)
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
-
Feb 8th, 2010, 10:48 AM
#5
Re: Capturing a timers time?
try this
Code:
Public Class Form1
Private Display As Boolean
Private WithEvents tmr As Windows.Forms.Timer
Private NewChar As Char
Private fnt As New Font(Me.Font.FontFamily, 72, FontStyle.Bold, GraphicsUnit.Point)
Dim MyRandom As New Random
Dim stpw As New Stopwatch
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Display = False
Me.Invalidate()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tmr = New Windows.Forms.Timer
Display = True
Me.KeyPreview = True
tmr.Interval = 10
tmr.Start()
End Sub
Private Sub Timer_Tick(ByVal sender As Object, _
ByVal e As EventArgs) Handles tmr.Tick
'Get a random time between 7 and 20 seconds for the timer
tmr.Interval = MyRandom.Next(7, 21) * 1000
' Get a random number between A(65) and Z(90).
NewChar = Chr(MyRandom.Next(65, 91))
' Make sure screen gets repainted
Me.Invalidate()
' Stop the timer so that it doesn't fetch the next character automatically
tmr.Stop()
stpw.Reset()
stpw.Start()
End Sub
' Stopping and restarting timer
Private Sub Form1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (Char.ToUpper(e.KeyChar) = NewChar) Then
stpw.Stop()
txtRecord.Text = stpw.Elapsed.TotalSeconds.ToString
NewChar = ControlChars.NullChar
tmr.Start()
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Display Then Dim textSize As SizeF = e.Graphics.MeasureString(NewChar, fnt)
If NewChar = ControlChars.NullChar Then Exit Sub
e.Graphics.DrawString(NewChar, fnt, Brushes.Red, x:=50, y:=50)
End Sub
End Class
-
Feb 9th, 2010, 09:16 AM
#6
Thread Starter
Member
Re: Capturing a timers time?
Seems to work 
What would be the best way to catch subsequent attempts as currently the result gets overwritten?
-
Feb 9th, 2010, 09:45 AM
#7
Re: Capturing a timers time?
add the stopwatch time to a listbox, or a List of timespan, or a richtextbox, etc.
-
Feb 10th, 2010, 09:26 AM
#8
Thread Starter
Member
Re: Capturing a timers time?
I can't get it to work with a list box
Using a rich text box just ends up with the same problem
-
Feb 10th, 2010, 11:03 AM
#9
Re: Capturing a timers time?
 Originally Posted by rory-uk
I can't get it to work with a list box
Using a rich text box just ends up with the same problem
Try this
Code:
Public Class Form1
Private Display As Boolean
Private WithEvents tmr As Windows.Forms.Timer
Private NewChar As Char
Private fnt As New Font(Me.Font.FontFamily, 72, FontStyle.Bold, GraphicsUnit.Point)
Dim MyRandom As New Random
Dim stpw As New Stopwatch
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Display = False
Me.Invalidate()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tmr = New Windows.Forms.Timer
Display = True
Me.KeyPreview = True
tmr.Interval = 10
tmr.Start()
End Sub
Private Sub Timer_Tick(ByVal sender As Object, _
ByVal e As EventArgs) Handles tmr.Tick
'Get a random time between 7 and 20 seconds for the next timer interval
tmr.Interval = MyRandom.Next(1, 5) * 1000
' Get a random number between A(65) and Z(90).
NewChar = Chr(MyRandom.Next(65, 91))
' Make sure screen gets repainted
Me.Invalidate()
' Stop the timer so that it doesn't fetch the next character automatically
tmr.Stop()
stpw.Reset()
stpw.Start()
End Sub
' Stopping and restarting timer
Private Sub Form1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (Char.ToUpper(e.KeyChar) = NewChar) Then
stpw.Stop()
e.Handled = True
RichTextBox1.AppendText(stpw.Elapsed.TotalSeconds.ToString("N3") & Environment.NewLine)
NewChar = ControlChars.NullChar
tmr.Start()
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Display Then Dim textSize As SizeF = e.Graphics.MeasureString(NewChar, fnt)
If NewChar = ControlChars.NullChar Then Exit Sub
e.Graphics.DrawString(NewChar, fnt, Brushes.Red, x:=50, y:=50)
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|