wpfvb.netvb.net-2010

how to use textChanged event when from barcode reader


I am using a Barcode reader to get bar code from products . i get the code in a textfield . and i put textchanged event on that textbox . but the problem is that when bar code puts it value ( for example if bar code is 5 digit ) then textchanged event is fired five times . how to get ride of this thing ???


Solution

  • You should be able to program your Barcode reader to output a prefix character and a suffix character (one output before the scanned value and one afterwards). Let's say that you set it up to output an asterisk (*) before the scanned data value and output a carriage return (CR) afterwards. Attach a handler to the TextBox.PreviewTextInput Event and listen out for the asterisk character:

    private void PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        if (e.Text == "*") 
        {
            e.Handled = true;
            // Data input has started
        }
    }
    

    You can use this to pop up a message saying 'Scanning...', or anything else that you require. Next, attach a handler to the TextBox.KeyUp Event and listen out for the Enter key:

    private void KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {
            string scannedValue = ScanTextBox.Text.Replace("*", string.Empty);
            // Do something with scannedValue 
        }
    }
    

    Now the scannedValue variable should contain the scanned barcode value.