Results 1 to 6 of 6

Thread: [RESOLVED] How to detect when the user stops typing

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Resolved [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?
    .
    ~Peter


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    2.     'The user just added, edited or removed input so start or restart the Timer.
    3.     Timer1.Stop()
    4.     Timer1.Stop()
    5. End Sub
    6.  
    7. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    8.     'The user hasn't changed the input for the specified period.
    9.     'Stop the Timer, which will be started again next time the change the input.
    10.     Timer1.Stop()
    11.  
    12.     'Do the business, whatever that may be, here.
    13.     '...
    14. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up 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
    ~Peter


  4. #4
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: [RESOLVED] How to detect when the user stops typing

    vb Code:
    1. Timer1.Stop()
    2.     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?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How to detect when the user stops typing

    Quote Originally Posted by proneal View Post
    vb Code:
    1. Timer1.Stop()
    2.     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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up Re: [RESOLVED] How to detect when the user stops typing

    Yep, i caught that.
    ~Peter


Tags for this Thread

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