文本框中的扫描值(使用扫描仪)
我正在使用 Scanner
(基本型号)来扫描条形码。扫描的条形码将被捕获在文本框中。在 txtBarcode_TextChanged 事件中,我正在获取要访问的条形码。
问题:
如果我多次单击扫描仪,条形码会附加上一个值。
我的代码:
protected void txtBarcode_TextChanged(object sender, EventArgs e)
{
string txt = this.txtBarcode.Text;
this.txtBarcode.Text = string.Empty;
}
I am using Scanner
(basic model) to scan the barcode. Scanned barcode will be captured in a textbox. In txtBarcode_TextChanged
event, I am getting the barcode to access.
Problem:
If I click the scanner more than once, the barcode gets append with the previous value.
My Code:
protected void txtBarcode_TextChanged(object sender, EventArgs e)
{
string txt = this.txtBarcode.Text;
this.txtBarcode.Text = string.Empty;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
条形码扫描仪的特点是它们通常看起来像标准的 HID 键盘。因此,扫描的每个新代码实际上都是在前一个代码之后“键入”的。我过去使用的解决方案是查看该文本框中的按键之间经过了多少时间。如果它超过 10 毫秒(或大约该值,我相信这是我用来“输入”整个代码的扫描仪所花费的最大时间),那么它是一个新的条形码,您应该删除它之前的所有内容。
我手边没有 IDE,所以大多数类/方法名称可能都离我很远,但就像一个例子:
我认为应该可以做到。它之所以有效,是因为 KeyPress 事件发生在附加字符之前,因此您可以先清除文本框。
编辑:要进行设置,我想无论您在哪里有
txtBarcode.TextChanged += txtBarcode_TextChanged
,您都会有一个txtBarcode.KeyPress += txtBarcode_KeyPress
。检查事件名称是否正确。编辑 2:
jQuery 版本:
假设此 HTML(由于您使用的是 ASP,输入标记的源代码看起来会有所不同,但输出仍将具有
id
属性,这确实是唯一重要的):然后这个 javascript 起作用了:
似乎(至少在网络浏览器中)50 毫秒是字符之间允许的所需时间。我已经在 Firefox、Chrome 和 IE7 中对此进行了测试。
The thing with barcode scanners is that they usually present themselves looking like a standard HID keyboard. Therefore, each new code scanned is effectively 'typed' after the previous one. A solution I have used in the past is to see how much time passes between key presses in that textbox. If it's more than 10 milliseconds (or around that value, I believe this was the largest amount of time taken for the scanner I was using to 'type' an entire code), then it's a new barcode, and you should delete everything before it.
I haven't got an IDE to hand, so most of the class/method names are probably way off, but something like an example:
I think that should do it. It works because the KeyPress event occurs before the character is appended, so you can clear the textbox first.
Edit: To set up, I guess that wherever you have
txtBarcode.TextChanged += txtBarcode_TextChanged
, you instead have atxtBarcode.KeyPress += txtBarcode_KeyPress
. Check the event name is right though.Edit 2:
jQuery Version:
Assuming this HTML (since you are using ASP, your source for the input tag will look different, but the output will still have the
id
attribute, which is really the only one that matters):Then this javascript works:
It seems that (at least in a web browser) 50 milliseconds is the required time to allow between characters. I've tested this in Firefox, Chrome and IE7.
尝试将 TextChanged 事件处理程序更改为以下类型:
它将在读取代码后选择文本框中的文本,并在其他读取时重写它。 + 会更适合用户复制或手工更改
try to change TextChanged event handler to kind of this:
It will select text in textbox after reading the code and rewrite it on other read. + it will be more suitable for users to copy it or change by hand
大多数扫描仪都可以编程为在扫描后“按 Enter”,请查看用户手册。您可以使用 Keypress 或 Keydown 事件处理程序来检查“enter”键并将其用作条形码的分隔符。如果您愿意,还可以使用特殊的分隔字符。
Most scanners can be programmed to "press enter" after scanning, check your user manual. You can use a Keypress or Keydown event handler to check for the "enter" key and use it as delimiter for your barcode. You can also use a special delimiting character if you prefer.
如果您要分配
txtBarcode.value +=barcode
,请将其更改为txtBarcode.value =barcode
If you're assigning
txtBarcode.value += barcode
, change it totxtBarcode.value = barcode