D
Dan Bass
I'm writing a scanner application (C# / .Net CF) where I have one scanner
object (singleton) that hooks into the scan engine drivers.
There is scanner event for a successful scan, that passes through the
scanned data to an event handler. From here I need to pass this information
back to my form, so what I've done is to create another event handler in the
form, which is fired from the event handler in my scanner class.
The Form declaration of this object looks like this:
Is this ok?
Is this considered good practice or is there a blatantly obvious and much
better way of doing this?
Below is the code that's relevant:
// ----------------------------------------------------------
//
// myForm snippet
//
ScanEngine scanner;
private void myForm_Load(object sender, System.EventArgs e)
{
scanner = new ScanEngine();
scanner.StartScan();
scanner.Scanned += new EventHandler(BarcodeRead);
}
private void FormPut_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if ( scanner != null )
{
scanner.StopScan();
scanner.Scanned -= new EventHandler(BarcodeRead);
scanner = null;
}
}
void BarcodeRead(object sender, System.EventArgs e)
{
if ( scanner != null )
{
string barData = scanner.BarcodeData;
// do stuff here
}
}
// ----------------------------------------------------------
//
// ScanEngine snippet
//
private string barData = "";
public event System.EventHandler Scanned;
// called when a scan occurs
protected virtual void OnScanned(EventArgs e)
{
if (Scanned != null)
Scanned(this, e);
}
public string BarcodeData
{
get
{
return barData;
}
}
object (singleton) that hooks into the scan engine drivers.
There is scanner event for a successful scan, that passes through the
scanned data to an event handler. From here I need to pass this information
back to my form, so what I've done is to create another event handler in the
form, which is fired from the event handler in my scanner class.
The Form declaration of this object looks like this:
Is this ok?
Is this considered good practice or is there a blatantly obvious and much
better way of doing this?
Below is the code that's relevant:
// ----------------------------------------------------------
//
// myForm snippet
//
ScanEngine scanner;
private void myForm_Load(object sender, System.EventArgs e)
{
scanner = new ScanEngine();
scanner.StartScan();
scanner.Scanned += new EventHandler(BarcodeRead);
}
private void FormPut_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if ( scanner != null )
{
scanner.StopScan();
scanner.Scanned -= new EventHandler(BarcodeRead);
scanner = null;
}
}
void BarcodeRead(object sender, System.EventArgs e)
{
if ( scanner != null )
{
string barData = scanner.BarcodeData;
// do stuff here
}
}
// ----------------------------------------------------------
//
// ScanEngine snippet
//
private string barData = "";
public event System.EventHandler Scanned;
// called when a scan occurs
protected virtual void OnScanned(EventArgs e)
{
if (Scanned != null)
Scanned(this, e);
}
public string BarcodeData
{
get
{
return barData;
}
}