Results 1 to 9 of 9

Thread: Capturing a timers time?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    39

    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?

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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:
    Code:
    TimerStart = Now
    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

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    39

    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

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    39

    Re: Capturing a timers time?

    Seems to work

    What would be the best way to catch subsequent attempts as currently the result gets overwritten?

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Capturing a timers time?

    add the stopwatch time to a listbox, or a List of timespan, or a richtextbox, etc.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    39

    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

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Capturing a timers time?

    Quote Originally Posted by rory-uk View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width