.NET --- 文本框控件 - 等到用户完成输入

.NET --- Textbox control - wait till user is done typing(.NET --- 文本框控件 - 等到用户完成输入)
本文介绍了.NET --- 文本框控件 - 等到用户完成输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

是否有内置方法可以知道用户何时在文本框中完成输入?(在点击选项卡或移动鼠标之前)我有一个发生在 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"

推荐答案

一种方法:

  1. 创建一个 TimerInterval 为 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

可选:处理 LeaveKeyDown 事件,以便离开控件或按 EnterStop() 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.

注意:TextBoxes和Timers之间必须是一对一的配对;如果您打算使用多个输入来执行此操作,我会考虑构建一个包装此功能的 UserControl.

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.

这篇关于.NET --- 文本框控件 - 等到用户完成输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)