条形码扫描后的列表框事件

本文关键字:事件 列表 扫描 条形码 | 更新日期: 2023-09-27 18:13:02

我在窗口应用程序中使用列表框,我的列表框有一些条形码。

我想通过条形码阅读器扫描它们,然后想将它们移动到另一个列表框中,但我无法找到列表框的任何事件,自动触发并移动条形码到另一个列表框。

条形码扫描后的列表框事件

这适用于我的扫描仪,如果ListBox有焦点:

string scannerInput = "";
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((int)e.KeyChar == 13)
    {
        listBox1.Items.Add(scannerInput );
        scannerInput = "";
    }
    else scannerInput += e.KeyChar.ToString();
}

大多数条码扫描器会添加一个"载波返回",即在读取条码后输入

就像模拟按输入键。

你可以试着捕捉这个在表单或列表框控件的keypress/keydown事件上输入(如果列表框处于焦点中)

让一个控件处理来自键盘楔的条形码扫描是有问题的。要求用户在扫描前将焦点设置在控件上是自找麻烦。尝试通过实现"PreviewTextInput"事件让表单处理扫描。

大多数键盘楔形扫描器可以编程发送一个前导码和后导码。这些应该是不可打印的ASCII字符。我用过Char(2)和Char(3)。它们分别是STX -文本的开始和ETX -文本的结束。

bool InteceptBarcode = false;
string barcodeValue = string.empty;
private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if(InteceptBarcode)
    {
        barcodeValue = += e.Text
        e.Handled = true; //The keyboard character will stop bubbling up the control tree 
    }
    else if (e.Text == (char)2  //Start of text character
    {
        InterceptBarcode = true;
        barcodeValue = string.empty;
        e.Handled = true;
    }
    else if (e.Text == {char)3) //End of text character
    {
            InterceptBarcode = false
        e.Handled = true;
        //Now do what ever you need to do on the UI.
    }
    else
    {
        e.Handled = false;
    }                   
}