origin origin2
origin origin2

Reputation: 73

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 ?

Upvotes: 5

Views: 20004

Answers (4)

AMSP
AMSP

Reputation: 81

You can configure your Barcode reader to "Add an enter key" or "Add a tab key" after scanning the bar code. Then you can use it as below.

private void txtBarcode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
   if (e.KeyCode == Keys.Enter)
   {
       //Do Something
   }
}

Upvotes: 8

Norcino
Norcino

Reputation: 6627

I did the same years ago. First of all check whether or not your reader sends a final carriage return char, after the actual barcode string. Usually along with the reader you have certain barcode that you can use to configure the device.

If you are not lucky with the device, make the event implementation asynchronous, and wait 200ms before running the SQL. Than if another event is raised by the waiting time, change the string used for the search or just abort the old event and create a new one.

This should work because the barcode is a keyboard that push sequence of chars through the I/O, at high speed (less than 200ms for sure).

I hope this will help.

Upvotes: 1

mittmemo
mittmemo

Reputation: 2090

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.

Upvotes: 3

John Koerner
John Koerner

Reputation: 38087

Most barcode scanners have the ability to add a key sequence to the end of the scanned data. Many simply use a CRLF. You could listen for this on the keypress event in the textbox and then use that to run your SQL code.

The motorola scanners usually have a quick start guide that have barcodes to scan to set this up.

Upvotes: 1

Related Questions