0

I have a barcode scanner which reads the string of the barcode and displays in the active text box. The issue I am having, is that I need that barcode to be used as soon as its scanned (no user "ok" button).

When I do the Text Changed event, it fires as soon as the first character of the barcode is entered into the text box. (i.e if the barcode is 123r54122, it fires with '1' in the text box).

There is no consistent end character to the barcode, or standard length. So how would I go about firing a method when the WHOLE string has been read in?

skaffman
  • 398,947
  • 96
  • 818
  • 769
MichaelMcCabe
  • 523
  • 4
  • 15
  • 33

4 Answers4

4

You can verify text length (I think it is constant for bar codes). E.g. subscribe to TextChange event and if text length = barCodeLength then raise Scanned event.

If bar code has variable length you can try something like this: 1) define

private Timer _timer;
private DateTime _lastBarCodeCharReadTime;

2) initialize timer

_timer = new Timer();
_timer.Interval = 1000;
_timer.Tick += new EventHandler(Timer_Tick);

3) add handler

private void Timer_Tick(object sender, EventArgs e)
{
    const int timeout = 1500;
    if ((DateTime.Now - _lastBarCodeCharReadTime).Milliseconds < timeout)
        return;

    _timer.Stop();
    // raise Changed event with barcode = textBox1.Text            
}

4) in TextChanged event handler add this

private void textBox1_TextChanged(object sender, EventArgs e)
{    
    _lastBarCodeCharReadTime = DateTime.Now;
    if (!_timer.Enabled)
        _timer.Start();
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Then you should 1) wait for some custom character from barcode reader (e.g. NewLine) 2) wait for some event from barcode reader 3) define some timeout for new character appearing and raise Scanned event if timeout elapsed but no new characters was added. E.g. start timer after receiving textchanged event, store current datetime and in timer verify if text was changed. – Sergey Berezovskiy Jan 06 '11 at 12:14
1

The barcode scanners I've worked with add a newline (return/enter) to the end of the barcode string. Set the textbox to accept return (AcceptReturn to true) and then do something like

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
   if (e.KeyChar == (char)Keys.Return)
      doSomething();
}
dandan78
  • 13,328
  • 13
  • 64
  • 78
1

The only barcode scanner I have used (a USB model from Lindy) can append a return or not depending on how it is configured. Switching between modes is achieved by scanning a special control bar code printed on a leaflet provided with the scanner.

I'm not familiar with C# but in Java you can listen for an ActionEvent instead of a TextEvent to detect when return is pressed as opposed to a character being typed. This would be a simpler alternative to dandan78's suggestion, if it is available in C#.

Ben
  • 2,348
  • 2
  • 19
  • 28
0

Does the scanner not send a signal indicating it's completed reading the information? It surely would if it doesn't have a standard length of ending character. Anyway, you should read in the value into memory, and then set the textbox text at once rather than inserting each character as it's recieved.

Edit; If you're writing the information into a textbox as you recieve it, then calling the textbox event.. why bother writing it to the text box? Just call the event when you've determined it's a complete barcode directly

Rob
  • 26,989
  • 16
  • 82
  • 98
  • Its a third party app which takes what is scanned and enters it into whatever live text box. As far as im aware, i dont have control over it (knowing when its finished etc) – MichaelMcCabe Jan 06 '11 at 12:10
  • You could possibly create a timer to wait until data stops coming. Only feasible way I can think of if there's no set limit, no ending character and no ending signal.. albeit a bit 'hacky' – Rob Jan 06 '11 at 12:16
  • Yeah. I think I am going to have to do the timer idea – MichaelMcCabe Jan 06 '11 at 12:36