.NET — Textbox control - wait till user is done typing

前端 未结 6 850
轻奢々
轻奢々 2020-12-03 05:29

Greetings all,

Is there a built in way to know when a user is done typing into a textbox? (Before hitting tab, Or moving the mouse) I have a database query that occ

6条回答
  •  遥遥无期
    2020-12-03 06:04

    One approach:

    1. Create a Timer with an Interval of X milliseconds

      The interval should be about 300ms; more than a normal time between keystrokes, and also reasonable time to wait between finishing and the update occurring

    2. In the input's TextChanged event, Stop() and then Start() the Timer

      This will restart the Timer if it is already running, so if the user keeps typing at a normal rate, each change will restart the timer.

    3. In the timer's Tick event, Stop() the Timer and do the long transaction

    4. Optional: Handle the Leave and KeyDown events so that leaving the control or pressing Enter will Stop() the Timer and do the long transaction.

    This will cause an update if the text has changed, and the user hasn't made any changes in X milliseconds.

    One problem with the "Measure the time since the last update" approach you're considering is that if the last change is made quickly, the update won't happen, and there won't be any subsequent changes to trigger another check.

    Note: There must be a one to one pairing between TextBoxes and Timers; if you're planning on doing this with more than one input, I'd consider building a UserControl that wraps this functionality.

提交回复
热议问题