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
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
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
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
Re: Capturing a timers time?
Seems to work :)
What would be the best way to catch subsequent attempts as currently the result gets overwritten?
Re: Capturing a timers time?
add the stopwatch time to a listbox, or a List of timespan, or a richtextbox, etc.
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
Re: Capturing a timers time?
Quote:
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