文本框中的扫描值(使用扫描仪)

发布于 2024-09-02 21:10:45 字数 352 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

第七度阳光i 2024-09-09 21:10:45

条形码扫描仪的特点是它们通常看起来像标准的 HID 键盘。因此,扫描的每个新代码实际上都是在前一个代码之后“键入”的。我过去使用的解决方案是查看该文本框中的按键之间经过了多少时间。如果它超过 10 毫秒(或大约该值,我相信这是我用来“输入”整个代码的扫描仪所花费的最大时间),那么它是一个新的条形码,您应该删除它之前的所有内容。

我手边没有 IDE,所以大多数类/方法名称可能都离我很远,但就像一个例子:

DateTime lastKeyPress = DateTime.Now;

void txtBarcode_KeyPress(object sender, KeyPressEventArgs args)
{

   if(((TimeSpan) (DateTime.Now - lastKeyPress)).TotalMilliseconds > 10)
   {
     txtBarcode.Text = "";      
   }
   lastKeyPress = DateTime.Now;
}

我认为应该可以做到。它之所以有效,是因为 KeyPress 事件发生在附加字符之前,因此您可以先清除文本框。

编辑:要进行设置,我想无论您在哪里有txtBarcode.TextChanged += txtBarcode_TextChanged,您都会有一个txtBarcode.KeyPress += txtBarcode_KeyPress 。检查事件名称是否正确。

编辑 2


jQuery 版本:

假设此 HTML(由于您使用的是 ASP,输入标记的源代码看起来会有所不同,但输出仍将具有 id 属性,这确实是唯一重要的):

   <form action="" method="post">
        <input type="text" name="txtBarcode" id="txtBarcode" />
    </form>

然后这个 javascript 起作用了:

$(document).ready(function() {

   var timestamp = new Date().getTime();

   $("#txtBarcode").keypress(function(event)
   {
        var currentTimestamp = new Date().getTime();

        if(currentTimestamp - timestamp > 50)
        {
            $(this).val("");
        }
        timestamp = currentTimestamp;
   });                                

});

似乎(至少在网络浏览器中)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:

DateTime lastKeyPress = DateTime.Now;

void txtBarcode_KeyPress(object sender, KeyPressEventArgs args)
{

   if(((TimeSpan) (DateTime.Now - lastKeyPress)).TotalMilliseconds > 10)
   {
     txtBarcode.Text = "";      
   }
   lastKeyPress = DateTime.Now;
}

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 a txtBarcode.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):

   <form action="" method="post">
        <input type="text" name="txtBarcode" id="txtBarcode" />
    </form>

Then this javascript works:

$(document).ready(function() {

   var timestamp = new Date().getTime();

   $("#txtBarcode").keypress(function(event)
   {
        var currentTimestamp = new Date().getTime();

        if(currentTimestamp - timestamp > 50)
        {
            $(this).val("");
        }
        timestamp = currentTimestamp;
   });                                

});

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.

傲性难收 2024-09-09 21:10:45

尝试将 TextChanged 事件处理程序更改为以下类型:

txtBarcode.SelectionStart = 0;  
txtBarcode.SelectionLength = txtBarcode.Text.Length;

它将在读取代码后选择文本框中的文本,并在其他读取时重写它。 + 会更适合用户复制或手工更改

try to change TextChanged event handler to kind of this:

txtBarcode.SelectionStart = 0;  
txtBarcode.SelectionLength = txtBarcode.Text.Length;

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

人海汹涌 2024-09-09 21:10:45

大多数扫描仪都可以编程为在扫描后“按 Enter”,请查看用户手册。您可以使用 Keypress 或 Keydown 事件处理程序来检查“enter”键并将其用作条形码的分隔符。如果您愿意,还可以使用特殊的分隔字符。

 private void txtScan_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
               //Do something here...

                txtScan.Text = "";
                txtScan.Focus(FocusState.Programmatic);
                e.Handled = true;  //keeps event from bubbling to next handler
            }
        }

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.

 private void txtScan_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
               //Do something here...

                txtScan.Text = "";
                txtScan.Focus(FocusState.Programmatic);
                e.Handled = true;  //keeps event from bubbling to next handler
            }
        }
把昨日还给我 2024-09-09 21:10:45
<html>

<body>
  <script>
    var _lastRead = new Date();

    function Validate(control) {
      var _now = new Date();
      if ((_now - _lastRead) > 10) {
        control.value = "";
      }
      _lastRead = new Date();
    }
  </script>
  <input type="text" id="txtInput" onkeypress="Validate(this);" />
</body>

</html>

<html>

<body>
  <script>
    var _lastRead = new Date();

    function Validate(control) {
      var _now = new Date();
      if ((_now - _lastRead) > 10) {
        control.value = "";
      }
      _lastRead = new Date();
    }
  </script>
  <input type="text" id="txtInput" onkeypress="Validate(this);" />
</body>

</html>

掌心的温暖 2024-09-09 21:10:45

如果您要分配 txtBarcode.value +=barcode,请将其更改为 txtBarcode.value =barcode

If you're assigning txtBarcode.value += barcode, change it to txtBarcode.value = barcode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文