条形码扫描仪将值读取到文本框中的问题

发布于 2024-10-10 19:12:10 字数 214 浏览 3 评论 0原文

我有一个条形码扫描仪,它可以读取条形码字符串并显示在活动文本框中。 我遇到的问题是,我需要在扫描后立即使用该条形码(没有用户“确定”按钮)。

当我执行“文本更改”事件时,一旦在文本框中输入条形码的第一个字符,该事件就会触发。 (即,如果条形码是 123r54122,则会在文本框中显示“1”)。

条形码没有一致的结束字符或标准长度。那么,当读入整个字符串时,我将如何触发一个方法呢?

I have a barcode scanner which reads the string of the barcode and displays in the active text box.
The issue I am having, is that I need that barcode to be used as soon as its scanned (no user "ok" button).

When I do the Text Changed event, it fires as soon as the first character of the barcode is entered into the text box. (i.e if the barcode is 123r54122, it fires with '1' in the text box).

There is no consistent end character to the barcode, or standard length. So how would I go about firing a method when the WHOLE string has been read in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

弄潮 2024-10-17 19:12:10

您可以验证文本长度(我认为它对于条形码来说是恒定的)。
例如,订阅 TextChange 事件,如果文本长度 = barCodeLength,则引发 Scanned 事件。

如果条形码的长度可变,您可以尝试如下操作:
1) 定义

private Timer _timer;
private DateTime _lastBarCodeCharReadTime;

2) 初始化计时器

_timer = new Timer();
_timer.Interval = 1000;
_timer.Tick += new EventHandler(Timer_Tick);

3) 添加处理程序

private void Timer_Tick(object sender, EventArgs e)
{
    const int timeout = 1500;
    if ((DateTime.Now - _lastBarCodeCharReadTime).Milliseconds < timeout)
        return;

    _timer.Stop();
    // raise Changed event with barcode = textBox1.Text            
}

4) 在 TextChanged 事件处理程序中添加此

private void textBox1_TextChanged(object sender, EventArgs e)
{    
    _lastBarCodeCharReadTime = DateTime.Now;
    if (!_timer.Enabled)
        _timer.Start();
}

You can verify text length (I think it is constant for bar codes).
E.g. subscribe to TextChange event and if text length = barCodeLength then raise Scanned event.

If bar code has variable length you can try something like this:
1) define

private Timer _timer;
private DateTime _lastBarCodeCharReadTime;

2) initialize timer

_timer = new Timer();
_timer.Interval = 1000;
_timer.Tick += new EventHandler(Timer_Tick);

3) add handler

private void Timer_Tick(object sender, EventArgs e)
{
    const int timeout = 1500;
    if ((DateTime.Now - _lastBarCodeCharReadTime).Milliseconds < timeout)
        return;

    _timer.Stop();
    // raise Changed event with barcode = textBox1.Text            
}

4) in TextChanged event handler add this

private void textBox1_TextChanged(object sender, EventArgs e)
{    
    _lastBarCodeCharReadTime = DateTime.Now;
    if (!_timer.Enabled)
        _timer.Start();
}
猫七 2024-10-17 19:12:10

我使用过的条形码扫描仪会在条形码字符串的末尾添加换行符(回车/输入)。将文本框设置为接受返回(AcceptReturn 为 true),然后执行类似的操作

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
   if (e.KeyChar == (char)Keys.Return)
      doSomething();
}

The barcode scanners I've worked with add a newline (return/enter) to the end of the barcode string. Set the textbox to accept return (AcceptReturn to true) and then do something like

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
   if (e.KeyChar == (char)Keys.Return)
      doSomething();
}
御弟哥哥 2024-10-17 19:12:10

我使用过的唯一条形码扫描仪(Lindy 的 USB 型号)可以附加或不附加回车,具体取决于它的配置方式。模式之间的切换是通过扫描扫描仪附带的传单上打印的特殊控制条形码来实现的。

我不熟悉 C#,但在 Java 中,您可以侦听 ActionEvent 而不是 TextEvent,以检测何时按下回车键而不是何时键入字符。如果在 C# 中可用,这将是 dandan78 建议的更简单的替代方案。

The only barcode scanner I have used (a USB model from Lindy) can append a return or not depending on how it is configured. Switching between modes is achieved by scanning a special control bar code printed on a leaflet provided with the scanner.

I'm not familiar with C# but in Java you can listen for an ActionEvent instead of a TextEvent to detect when return is pressed as opposed to a character being typed. This would be a simpler alternative to dandan78's suggestion, if it is available in C#.

倦话 2024-10-17 19:12:10

扫描仪是否没有发出信息读取完毕的信号?如果它没有标准长度的结束字符,那肯定会的。
无论如何,您应该将值读入内存,然后立即设置文本框文本,而不是在收到每个字符时插入它。

编辑;如果您在收到信息时将信息写入文本框,然后调用文本框事件..为什么还要将其写入文本框呢?当你确定它是一个完整的条形码时,直接调用该事件即可

Does the scanner not send a signal indicating it's completed reading the information? It surely would if it doesn't have a standard length of ending character.
Anyway, you should read in the value into memory, and then set the textbox text at once rather than inserting each character as it's recieved.

Edit; If you're writing the information into a textbox as you recieve it, then calling the textbox event.. why bother writing it to the text box? Just call the event when you've determined it's a complete barcode directly

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