1

I have barcode scanner. I need to capture string from barcode into textbox. The final word in textbox will be composed of multiple codes from barcode scanner. What event(s) should I use?

For example : Barcode sends value 123 .

I tried PreviewTextInput event and e.Text but it captured only first char 1 . How can I get all added chars to textbox when I use barcode scanner?

1
  • 1
    Have a look at this link: dotnettrails.wordpress.com/2010/03/11/… It gives you two options: Either use a timer to delay the text_changed event, or capture the enter key on the keyUp event (it assumes that the barcode scanner will send a carriage return at the end of the scan) Commented Jul 30, 2013 at 12:38

2 Answers 2

2

TextBox does not lose anything. If e.Text is "1" then it only means that your scanner sends characters to TextBox one by one. Meaning you will get one event per character added.

Your approach is really wierd tho. TextBox knows nothing about barcodes, scanners. etc. It simply displays text. Why do you ask it, if your scanner finished scanning? That makes no sense. Leave poor TextBox alone. If you want to know when the scanning procces is finished, than you should add an appropriate event to class, which actually reads barcodes. And then, after this event fires, you should access the resulting string via TextBox.Text or using other means (forexample by accessing a viewmodel property).

Edit: the thing with wpf events, is that your control needs to have "keyboard focus", if you want it to recieve keyboard events. If you show a MessageBox then your TextBox loses focus and, as a result, no longer recieves events. Again, leave the TextBox alone. You need to write a dedicated class (i.e. BarcodeReader) which will handle keyboard events for you and then fire an event when the barcode is read. You can use Keyboard class inside the BarcodeReader to handle buttons pressed.

Sign up to request clarification or add additional context in comments.

2 Comments

actually it loses it some way...Barcode scanner work like it emulate the keyboard and it sends key-pressed stream.. I supposed I will get one event per character, but when it gets barcode (or string) contained from 3 characters, the event is raised only once and textbox also have contains 1st character in the end... when I don't use event it gets all 3 characters (in event I just have one MessageBox.Show)
hmm not exactly what I wanted but you directed me in better way, thanks.. +1
1

If you are using C# in back end then you can have a look at this question: Reading bar codes using wpf application

1 Comment

my barcode works like that I suppose it emulate keyboard, so it sends stream of chars (or stream of some keypressed events) into the textbox.. When I don't use any event on textbox and use scanner then (f.e. I send code 123), there will be 123 in textbox.. But when I try to catch it in some event (TextChanged or PreviewTextInput) it catches only 1st char (1) and remaining 2 chars disappear

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.