Japster
Japster

Reputation: 1003

Barcode Scanner only displaying first digit of barcode

I'm developing an application that will be using a barcode scanner. Now the problem I'm having is that when I do scan the barcodes it only displays the first number of my barcode in the TEdit.

When I test the barcode itself by scanning it say to MS Word or Notepad it scans the whole barcode and displays the whole number sequence correct.

So is there any code that I need to write to make the scanner read more then 1 number from my barcode when it is being scanned with my delphi application?

I scan into a TEdit box and use the OnChange event to grab the scan value.

  procedure TfrmMain.edtWeightChange(Sender: TObject);
  begin
    ActiveWeight := StrToFloat(edtWeight.text);
  end;

I'm using Delphi XE6.

EDIT: I'm using an USB barcode scanner, and assumed that it works just as a keyboard would work. Therefore I did not write any other code. I was under the assumption that because it works just like a keybaord I would only need to put focus into the TEdit and then scan the barcode. The Onchange Event was so that the TEdit can detect when the barcode scanner scanned. Then I would store that string value as a number variable ActiveWeight

Upvotes: 1

Views: 1957

Answers (1)

whosrdaddy
whosrdaddy

Reputation: 11860

Do not use the OnChangeevent because it can be fired multiple times, use the OnKeyPress event instead:

procedure TfrmMain.edtWeightKeyPress(Sender: TObject var Key: Char);
begin
 if Key=#13 then
  ActiveWeight := StrToFloat(edtWeight.text);
end;

this code assumes that the scanner sends a Carriage return after scanning the barcode (which is normally the case).

One point to note is that you need to use TryStrToFloat to prevent erroneous user input (ie user presses enter in the TEdit without a value)

Upvotes: 8

Related Questions