【问题标题】:Ignore CR/LF characters embedded as part of barcode when scanned to a text box扫描到文本框时,忽略作为条形码一部分嵌入的 CR/LF 字符
【发布时间】:2022-03-01 13:50:44
【问题描述】:

我有一个 C# Winform 应用程序,它将供应商条形码读入一个文本框,并通过处理 Keydown 事件并查找“ENTER”键按下自动转到另一个文本框。 “ENTER”键字符由条码扫描器在读取条码的所有字符时自动附加。但是,我遇到了嵌入 CR/LF 字符的条形码问题,这会导致 Keydown 事件错误地将其检测为扫描完成并触发传输到下一个文本框。是否可以在读取所有条形码字符之前忽略 CR/LF 字符?或者也许只是确定是否所有字符都已被读取,以便可以将其合并到 Keydown 事件处理程序中,以正确确定何时触发传输到其他文本框?

【问题讨论】:

  • 扫描成多个隐藏文本框
  • 不清楚您使用的是什么品牌/型号的条码扫描仪,或者您为什么没有将您的扫描仪设置为 USB 串行设备。您可以考虑taking the tour 并提供minimal, reproducible example,其他人可以使用它来重现您面临的问题。
  • 如果您能够将条形码扫描仪设置为 USB 串行设备,以下可能会有所帮助:stackoverflow.com/questions/65957066/…

标签: c# winforms


【解决方案1】:

我们在下面使用它来捕获 UPS/USPS/FedEx 条形码。它被实例化为 Form 上的成员变量,然后 Form.KeyPreview 将 char(s) 发送到其上的 KeyPress。它使用人类无法快速键入这些字符的直觉,因此它必须是扫描。当它接收到正确的长度和与托运人的条形码格式之一匹配的模式时,它会引发条形码扫描事件。您也可以调整它以清除扫描中的虚假 CRLF。

using System;
using System.Diagnostics;

namespace YadaYada.LabelMaker.Library;

public class KeyReader
{
    public delegate void ScannedEventHandler(string trackingNumber);
    public static event ScannedEventHandler Scanned;

private static readonly object CurrentValueLock = new object();

private enum CurrentlyTrackingEnum
{
    None = 0,
    Ups = 1,
    Usps = 2,
}

private static CurrentlyTrackingEnum _currentlyTracking;
private static DateTime _lastPress = DateTime.MinValue;

public static void KeyPress(char pressed)
{

    lock (CurrentValueLock)
    {
        Debug.WriteLine($"Start:{CurrentValue}");
        if (DateTime.Now.Subtract(_lastPress).Ticks > 1000000) CurrentValue = string.Empty;
        _lastPress = DateTime.Now;
        CurrentValue += pressed.ToString().ToUpperInvariant();

        if (pressed == ']')
        {
            CurrentValue = string.Empty;
            _currentlyTracking = CurrentlyTrackingEnum.Usps;
        }
        else if (CurrentValue == "1Z")
        {
            _currentlyTracking = CurrentlyTrackingEnum.Ups;
        }

        if (_currentlyTracking == CurrentlyTrackingEnum.Ups)
        {
            if (CurrentValue.Length == "1Z52895E0392843135".Length)
            {
                OnScanned(CurrentValue);
                _currentlyTracking = CurrentlyTrackingEnum.None;
            }
        }
        else if (_currentlyTracking == CurrentlyTrackingEnum.Usps)
        {
            if (CurrentValue.Length == "9400110298370442324568".Length)
            {
                OnScanned(CurrentValue);
                _currentlyTracking = CurrentlyTrackingEnum.None;

            }
        }
        Debug.WriteLine($"End:{CurrentValue}");
    }
}

public static string CurrentValue { get; private set; } = String.Empty;

private static void OnScanned(string trackingnumber)
{
    Scanned?.Invoke(trackingnumber);
    CurrentValue = string.Empty;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多