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?
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();
}