Timing Text_change events issue.

WinDev

Member
Joined
Jul 10, 2012
Messages
9
Programming Experience
1-3
Here is the deal. I am timing the time elapsed between textchanged events and post the time into the same textbox that is being monitored for text_changed. Still following me? lol well I have attempted to eliminate the line elapsed through string.contains, because if I don't then I get a stack error for obvious reasons(infinite loop). Now looking at the code it seems like it should work in theory. Although it doesnt.

After clicking the button to start timing it correctly times the first event that the text is changed, and then does not work after that. So basically it only works once then doesn't post it after that when text is changed. After making changes im still stumped as it should work. Atleast the good thing is I got rid of the stack error lol. Any help would be appreciated.

PS. I am using the stopwatch class to do the timing.

VB.NET:
[LEFT]
Private Sub DisplayTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayTextBox.TextChanged 
      
 Dim elapsed As TimeSpan = Me.myStopwatch.Elapsed       
        Dim str As String       
         str = DisplayTextBox.Text        
        If str.Contains("<Wait>") = True Then      
'do nothing 
Else           
         DisplayTextBox.AppendText(String.Format(vbCrLf & elapsed.Milliseconds & "<mSeconds>" &))            
          myStopwatch.Restart()        

 End If      
 End Sub[/LEFT]

 
Basically, for every "normal" TextChanged event you are adding an "extra" TextChanged event, so you simply want to ignore every second event. I'm guessing that you won't be showing the time until the second TextChanged event, so you need to account for that too:
Private ReadOnly timer As New Stopwatch
Private ignoreEvent As Boolean

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    If Not ignoreEvent Then
        If timer.IsRunning Then
            Dim time = timer.Elapsed

            ignoreEvent = True
            'Update Text of TextBox.
            ignoreEvent = False
        End If
    End If

    timer.Restart()
End Sub
 
Thanks JMC! It works perfectly. This way seems like a much better option then using string.contains. I REALLY REALLY appreciate it :)
 
Back
Top