origin origin2 origin origin2 - 9 months ago 76
C# Question

C# barcode scanner and textbox with changed event

So I have barcode scanner and textbox with changed text event. What I'm trying to do is that when user scans the code, it goes into textbox after that I have code which does some SQL (it works fine). Problem is that texbox accepts only first charachter of code not entire string because of changed text event.

I want to have it so because then user does not need to press any additional buttons to insert product.
I tried to capture barcode, save it into string but that also does not work.

Is there anyway around this ?

Answer Source

If the length of the code is always the same, you can check the length in the text changed event and defer the database operation until the code is the correct length.

If the code is of variable length, then you may have to be more clever.

  • Perhaps you can use the focus change event instead of text change event, so that the database operation is not run until the text box loses focus.
  • Program the barcode scanner to append a certain character to the end of the string and defer the database operation until you receive that character.
  • Use a timer to defer the database operation. For example, maybe you know that the entire code will be entered within 500ms. Just wait 500ms and ditch the text changed event.