在特定文本框中读取条形码而不在c#中设置任何焦点?

时间:2013-10-31 06:34:09

标签: asp.net barcode

如何在不使用任何焦点的情况下在特定文本框中显示条形码。 如果我使用条形码阅读器读取数据,数据应显示在特定的文本框中。

2 个答案:

答案 0 :(得分:0)

您可以将Serial Port类与DataReceive事件一起使用。收到数据时,请在文本框中填写该数据。

SerialPort mySerialPort = new SerialPort("COM1"); //give your barcode serial port 
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();

private void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        txtBoxName.Text = indata;
    }

答案 1 :(得分:0)

我会添加一个javascript函数来监听window.keypress事件并识别条形码序列,如下所示:

<html>
<body>
<script type="text/javascript">
    document.currentBarcodeSequence = "";
    document.lastKeypress = new Date();
    var monitorBarcodes = function(e){
        //sequenceLimitMs should be set as low as possible to prevent capture of human keyed numbers.
        //200 allow testing without a barcode scanner, you could try a value of 50 with a scanner.
        var sequenceLimitMs = 200;
        var now = new Date();
        var elapsed = now - document.lastKeypress;
        document.lastKeypress = now;
        if(e.charCode >= 48 && e.charCode <= 57){
            //pressed key is a number
            if(elapsed < sequenceLimitMs || document.currentBarcodeSequence === ""){
                //event is part of a barcode sequence
                document.currentBarcodeSequence += (e.charCode - 48);

                if(document.currentBarcodeSequence.length > 1){
                    clearTimeout(document.printBarcodeTimeout);
                    document.printBarcodeTimeout = setTimeout("setBarcode()", sequenceLimitMs+10);
                }
            } else {
                document.currentBarcodeSequence = "" + (e.charCode - 48);
                clearTimeout(document.printBarcodeTimeout);
            }
        } else {
            document.currentBarcodeSequence = "";
            clearTimeout(document.printBarcodeTimeout);
        }
    }
    var setBarcode = function(){
        var barcodeInput = document.getElementById("barcode");
        barcodeInput.value = document.currentBarcodeSequence;
    }

    window.onkeypress = monitorBarcodes;
</script>
<input type="text" id="barcode"></input>
<input type="text"></input>
</body>
</html>

在Firefox,IE10,Chrome&amp;戏。

编辑:脚本的工作原理是假设扫描仪只是按照键盘的方式发送键序列。