简体   繁体   English

C#条形码扫描仪和带有更改事件的文本框

[英]C# barcode scanner and textbox with changed event

So I have barcode scanner and textbox with changed text event. 所以我有条形码扫描仪和带有更改文本事件的文本框。 What I'm trying to do is that when user scans the code, it goes into textbox after that I have code which does some SQL (it works fine). 我正在尝试做的是,当用户扫描代码时,它进入文本框后,我有代码执行一些SQL(它工作正常)。 Problem is that texbox accepts only first charachter of code not entire string because of changed text event. 问题是,由于更改了文本事件,texbox只接受代码的第一个字符串而不是整个字符串。

I want to have it so because then user does not need to press any additional buttons to insert product. 我希望如此,因为用户不需要按任何其他按钮来插入产品。 I tried to capture barcode, save it into string but that also does not work. 我试图捕获条形码,将其保存到字符串中,但这也不起作用。

Is there anyway around this ? 有没有办法解决 ?

You can configure your Barcode reader to "Add an enter key" or "Add a tab key" after scanning the bar code. 扫描条形码后,您可以将条形码阅读器配置为“添加回车键”或“添加标签键”。 Then you can use it as below. 然后你可以使用它如下。

private void txtBarcode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
   if (e.KeyCode == Keys.Enter)
   {
       //Do Something
   }
}

If the length of the code is always the same, you can check the length in the text changed event and defer the database operation until the code is the correct length. 如果代码的长度始终相同,则可以检查text changed事件的长度并推迟数据库操作,直到代码的长度正确为止。

If the code is of variable length, then you may have to be more clever. 如果代码长度可变,那么您可能必须更聪明。

  • Perhaps you can use the focus change event instead of text change event, so that the database operation is not run until the text box loses focus. 也许您可以使用焦点更改事件而不是文本更改事件,以便在文本框失去焦点之前不会运行数据库操作。
  • Program the barcode scanner to append a certain character to the end of the string and defer the database operation until you receive that character. 对条形码扫描器进行编程,将特定字符附加到字符串的末尾,并推迟数据库操作,直到收到该字符。
  • Use a timer to defer the database operation. 使用计时器推迟数据库操作。 For example, maybe you know that the entire code will be entered within 500ms. 例如,您可能知道整个代码将在500毫秒内输入。 Just wait 500ms and ditch the text changed event. 等待500毫秒,然后抛弃文本更改事件。

Most barcode scanners have the ability to add a key sequence to the end of the scanned data. 大多数条形码扫描仪都能够将密钥序列添加到扫描数据的末尾。 Many simply use a CRLF. 许多人只是简单地使用CRLF。 You could listen for this on the keypress event in the textbox and then use that to run your SQL code. 您可以在文本框中的按键事件上监听此操作,然后使用它来运行SQL代码。

The motorola scanners usually have a quick start guide that have barcodes to scan to set this up. 摩托罗拉扫描仪通常有一个快速入门指南,可以使用条形码进行扫描以进行设置。

I did the same years ago. 我几年前也这样做过。 First of all check whether or not your reader sends a final carriage return char, after the actual barcode string. 首先检查读者是否在实际条形码字符串后发送最终回车符。 Usually along with the reader you have certain barcode that you can use to configure the device. 通常与阅读器一起,您可以使用某些条形码来配置设备。

If you are not lucky with the device, make the event implementation asynchronous, and wait 200ms before running the SQL. 如果您对设备不满意,请使事件实现异步,并在运行SQL之前等待200ms。 Than if another event is raised by the waiting time, change the string used for the search or just abort the old event and create a new one. 与等待时间引发的其他事件相比,更改用于搜索的字符串或仅中止旧事件并创建新事件。

This should work because the barcode is a keyboard that push sequence of chars through the I/O, at high speed (less than 200ms for sure). 这应该有效,因为条形码是一个键盘,可以高速(小于200毫秒)推动字符序列通过I / O.

I hope this will help. 我希望这将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM