[RESOLVED] How to detect when the user stops typing
Hello.
I'd like you filter some results in a grid, and i want the user to be able to type a value into a textbox, and then the grid will be filtered by that value.
I don't need help with the grid or how to populate it. My question is in regards to the textbox; How can i tell when the user has stopped typing so i can automatically load the grid?
I thought about doing a refresh automatically in the TextChanged event, but that seems silly since the user could still be typing more characters.
Any ideas on how i can figure out when the user stops typing in a textbox?
.
Re: How to detect when the user stops typing
You can't detect that the user has stopped typing. You can only detect that user hasn't typed for specific period of time, for which you would use a Timer. You have to decide what sort of threshold you want to use but I would think that somewhere in the range of 200 - 500 milliseconds should be OK.
vb.net Code:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'The user just added, edited or removed input so start or restart the Timer.
Timer1.Stop()
Timer1.Stop()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'The user hasn't changed the input for the specified period.
'Stop the Timer, which will be started again next time the change the input.
Timer1.Stop()
'Do the business, whatever that may be, here.
'...
End Sub
Re: How to detect when the user stops typing
Interesting. I figured it would require a timer, but i keep thinking i would need to do fancy math to count the interval between keydown events or something in order to figure out when the user stopped typing.
I set the timer to 750 and tweaked your code, and came up with a working solution. Thanks!
Code:
Private Sub txtSample_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSample.TextChanged
'The user has just added, edited or removed a value, so restart the Timer.
tmrWatchTyping.Stop()
lblDisplay.Text = "User is typing something,..."
tmrWatchTyping.Start()
End Sub
Private Sub tmrWatchTyping_Tick(sender As System.Object, e As System.EventArgs) Handles tmrWatchTyping.Tick
'The user hasn't changed the input for the specified period, so stop the Timer (which will be started again the next time the user changes the input).
tmrWatchTyping.Stop()
lblDisplay.Text = "Typed value: " & txtSample.Text
End Sub
Re: [RESOLVED] How to detect when the user stops typing
vb Code:
Timer1.Stop()
Timer1.Start()
I think he meant Start, not two Stops.. :)
But at least his restarts the timer so another chance to see if use stopped typing occurs.
think about it.
Which way works more than one time?
Re: [RESOLVED] How to detect when the user stops typing
Quote:
Originally Posted by
proneal
vb Code:
Timer1.Stop()
Timer1.Start()
I think he meant Start, not two Stops.. :)
But at least his restarts the timer so another chance to see if use stopped typing occurs.
think about it.
Which way works more than one time?
Oops! Yes, that's what I meant. Hopefully the comment indicated that and MrGTI seems to have worked it out.
Re: [RESOLVED] How to detect when the user stops typing