TextBox:TextChanged 事件 - 输入 x 个字符时自动验证
我有一个可以从 USB 扫描仪读取条形码的应用程序。我想在特定文本框中输入条形码后立即进行数据库查找。
显然,我将把我的代码放入 TextChanged 事件处理程序中,但我有一个小问题,因为正在读取的条形码可能是 41 或 43 个字符。问题是,我如何知道正在输入哪种类型的代码。扫描仪只是将代码作为字符串输入到具有焦点的任何字段中 - 就像通过键盘输入一样 - 因此我无法查询扫描仪来确定代码长度。
到目前为止,我想到了两种方法:
- 检测第一个字符,等待 xx 秒,然后进行查找 - 留出足够的时间输入完整代码
- 检测第 41 个字符,等待 0.x 秒,然后进行查找。
好的,我确信这些会起作用,但是还有其他更有说服力的解决方案吗?
结论:
一些有用的建议,但是 Ondrej 为我的场景启发了最简单的解决方案。 43 个字符的代码有两个字符对于所有代码都是固定的 - 这两个字符的存在(或不存在)意味着我知道如果这两个字符存在但只存在 41 个字符,则要等待。
我只需要确认我的怀疑,即 41 个字符代码不能在这个特定位置包含这对字符 - 我相当确定情况确实如此。
I have an app that reads in a barcode from a USB scanner. I want to do a DB lookup as soon as the barcode is entered in a particular textbox.
Clearly, I'm going to put my code in the TextChanged event handler, but I have a slight problem in that the barcode being read could be either 41 or 43 characters. The question is, how will I know which type of code is being entered. The scanner simply enters the code into whatever field has focus as a string of characters - as if it were entered by a keyboard - so I can't query the scanner to determine the code length.
I've thought of two approaches so far:
- Detect first character, wait x.x seconds, and then do my lookup - allow enough time for a full code to be entered
- Detect the 41st character, wait 0.x seconds and then do my lookup.
OK, I'm sure these will work OK, but are there any more eloquent solutions?
CONCLUSION:
Some useful suggestions, however Ondrej has inspired the simplest solution for my scenario. The 43 char codes have two characters that are fixed for all codes - the presence (or not) of these two characters means I know to wait if these two characters are present but only 41 characters are present.
I just need to confirm my suspicion that the 41 character codes can't have this pair of characters in this particular position - which I'm fairly sure is the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种选择是在读取第 41 个字符后执行异步查找,同时仍在 UI 线程中等待另外两个字符。如果您点击该条形码,您可以停止等待并显示结果。或者,如果在查找过程中输入第 42 个字符,您可以中止查找,并在输入第 43 个字符时立即开始新的查找。但是,这假设没有 43 字符条形码以现有 41 字符条形码开头。
我还会查看条形码是否包含某种模式(例如:41 字符代码的第 9 个字符有一个 X,而 43 字符的代码则没有),并在输入时扫描该模式。
One option is to perform an Async lookup after the 41st character is read, while still waiting for two more characters in the UI thread. If you get a hit on that barcode you can stop waiting and show that result. Alternatively, if a 42nd character is entered while the lookup is in progress you can abort it and immediately begin a new lookup when the 43rd character is entered. This assumes, however, that no 43-character barcode begins with an existing 41-character barcode.
I'd also look and see if the barcodes contain some pattern (for example: 41-char codes have an X as the 9th character and 43-char codes do not), and scan for that as it's being entered.
您可以有一个计时器,当 1 秒内没有发生任何变化时,如果长度是 41 或 43 个字符,则进行查找。
You could have a timer and while no changes have occurred for say 1s to then do the lookup should it be 41 or 43 characters long.
我将数据库查找调用包装在后台工作者类中。
在 TextChanged 事件处理程序上,我要做的第一件事是如果后台工作人员正在工作,我会取消它。然后,如果文本长度是 41 或 43,我将在后台工作者中处理数据库调用。
这样,如果用户输入 41 个字符,则会发生呼叫,如果他们输入第 42 个字符,呼叫将被取消,但是如果他们输入 43,然后删除两个字符(以恢复到 41 个字符),您仍然会进行正确的查找。
I'd wrap the DB lookup call in a background worker class.
On the TextChanged event handler, the first thing I'd do is if the backgroundworker is working, I'd cancel it. Then if the text length is 41 or 43, I'd process the DB call in the backgroundworker.
This way if the user hits 41 characters, the call happens, if they type in a 42nd character the call is cancelled, however if they type in 43, then delete two characters (to get back down to 41 characters) you'll still be doing the proper lookup.