问题描述
大家好,
是否有内置方法可以知道用户何时在文本框中完成输入?(在点击选项卡或移动鼠标之前)我有一个发生在 textchanged 事件上的数据库查询,并且一切正常.但是,我注意到当然有一点延迟,因为如果用户快速输入文本框,程序正忙于对每个字符进行查询.所以我希望的是一种查看用户是否完成输入的方法.因此,如果他们键入a"并停止,则会触发一个事件.但是,如果他们键入一直",则事件会在 y 键后触发.
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 occurs on the textchanged event and everything works perfectly. However, I noticed that there is a bit of lag of course because if a user is quickly typing into the textbox the program is busy doing a query for each character. So what I was hoping for was a way to see if the user has finished typing. So if they type "a" and stop then an event fires. However, if they type "all the way" the event fires after the y keyup.
我有一些想法在我脑海中浮现,但我确信它们不是最有效的.就像测量自上次 textchange 事件以来的时间,如果大于某个值,那么它将继续运行我的其余程序.
I have some ideas floating around my head but I'm sure they aren't the most efficient. Like measuring the time since the last textchange event and if it was > than a certain value then it would proceed to run the rest of my procedures.
让我知道你的想法.
语言:VB.NET框架:.Net 2.0
Language: VB.NET Framework: .Net 2.0
--编辑澄清完成打字"
--Edited to clarify "done typing"
推荐答案
一种方法:
创建一个
Timer
,Interval
为 X 毫秒
间隔应该是300ms左右;超过正常的击键时间,以及完成和更新发生之间的合理等待时间
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
在输入的 TextChanged
事件中,Stop()
然后 Start()
Timer
In the input's TextChanged
event, Stop()
and then Start()
the Timer
如果 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.
在定时器的Tick
事件中,Stop()
Timer
并做长事务
In the timer's Tick
event, Stop()
the Timer
and do the long transaction
可选:处理 Leave
和 KeyDown
事件,以便离开控件或按 Enter 将 Stop()
Timer
并进行长事务.
Optional: Handle the Leave
and KeyDown
events so that leaving the control or pressing Enter will Stop()
the Timer
and do the long transaction.
如果文本已更改,并且用户在 X 毫秒内没有进行任何更改,这将导致更新.
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.
注意:TextBox
es和Timer
s之间必须是一对一的配对;如果您打算使用多个输入来执行此操作,我会考虑构建一个包装此功能的 UserControl
.
Note: There must be a one to one pairing between TextBox
es and Timer
s; if you're planning on doing this with more than one input, I'd consider building a UserControl
that wraps this functionality.
这篇关于.NET --- 文本框控件 - 等到用户完成输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!