Windows Develop Bookmark and Share   
 index > Windows Forms General > Class Event triggering a method on a Form...
 

Class Event triggering a method on a Form...

Hi all,

i have a Class that handles Barcode scanning functionality, and a Windows Form that contains an instance of this Barcode class. One of the Properties of the Barcode Class is an Event, which is triggered when the user executes a successful barcode scan. What I want is to be able to somehow call a method belonging to the Form from within the handled event that sits in Barcode class. How can I do this? The extremely watered down code is below...

// Barcode Class
class clsBarcodeScanner
{
// Declare and initialise EventsHandlers.
private System.EventHandler MyEventHandler = null;

private bool InitReader()
{
// Create event handler delegate
this.MyEventHandler = new EventHandler(MyReader_ReadNotify);
}

private void MyReader_ReadNotify(object sender, EventArgs e)
{
// If it is a successful read (as opposed to a failed one)
if (MyReaderData.Result == Symbol.Results.SUCCESS)
{
// WHAT TO CALL THE FORM METHOD HERE!
}
}
}

// Form Class
public class FormSnagList : System.Windows.Forms.Form
{
private clsBarcodeScanner m_clsBarcodeScanner;

public FormSnagList()
{
m_clsBarcodeScanner = new clsBarcodeScanner();
}

public void MethodWantToCall()
{
// THE METHOD I WANT TO CALL.
}
}

Hope you can help.

Thanks

Tryst
Tryst  Monday, May 22, 2006 8:30 PM

Tryst,glad to be of help!!

Can you kindly mark this as an answer?

Karthik Krishnaswami  Tuesday, May 23, 2006 2:30 PM
Hi,
see if this works for you:

// Barcode Class
class clsBarcodeScanner
{
// Declare and initialise EventsHandlers.
private System.EventHandler MyEventHandler = null;
// Declare a reference to the form
private
FormSnagList MyForm = null;

public
clsBarcodeScanner(FormSnagList form)
{
MyForm = form;
}

private bool InitReader()
{
// Create event handler delegate
this.MyEventHandler = new EventHandler(MyReader_ReadNotify);
}

private void MyReader_ReadNotify(object sender, EventArgs e)
{
// If it is a successful read (as opposed to a failed one)
if (MyReaderData.Result == Symbol.Results.SUCCESS)
{
if(MyForm != null)
MyForm.MethodWantToCall();
}
}
}

// Form Class
public class FormSnagList : System.Windows.Forms.Form
{
private clsBarcodeScanner m_clsBarcodeScanner;

public FormSnagList()
{
m_clsBarcodeScanner = new clsBarcodeScanner(this);
}

public void MethodWantToCall()
{
// THE METHOD I WANT TO CALL.
}
}


I added an attribute to the clsBarcodeScanner that will hold a reference to the form. This way you may call the method you want.
n0n4m3  Monday, May 22, 2006 10:53 PM

Hi Tryst,I think it may be a good idea to implement the Observer pattern here,since your clsBarCodeScanner should not need to know as to which method to call on the FormSnagList form.The approach should be that FormSnagList (or anybody else)should register its interest with the clsBarCodeScanner and whenever the state of clsBarCodeScanner changes,it will notify all objects who have interest.

Here is the code-

//Any object which wants other object to register its interest with itself will implement this interface

interface IObservable

{

void AddObserver(IObserver observer);

void Notify();

}

//Any object which wants to update itself when notified by the subject will implement this interface

interface IObserver

{

void Update();

}

class clsBarcodeScanner : IObservable //Allows objects to register itself

{

// Declare and initialise EventsHandlers.

private System.EventHandler MyEventHandler = null;

private ArrayList list = new ArrayList();

public void AddObserver(IObserver observer) //Add observers

{

list.Add(observer);

}

private bool InitReader()

{

// Create event handler delegate

this.MyEventHandler = new EventHandler(MyReader_ReadNotify);

return true;

}

private void MyReader_ReadNotify(object sender, EventArgs e)

{

// If it is a successful read (as opposed to a failed one)

if (MyReaderData.Result == Symbol.Results.SUCCESS)

{

Notify(); //Notify all observers

// WHAT TO CALL THE FORM METHOD HERE!

}

}

#region IObservable Members

public void Notify()

{

foreach (IObserver observer in list)

observer.Update(); //Loop through each observer asking it to update itself

}

// Form Class

public class FormSnagList : System.Windows.Forms.Form, IObserver

{

private clsBarcodeScanner m_clsBarcodeScanner;

public FormSnagList()

{

m_clsBarcodeScanner = new clsBarcodeScanner();

m_clsBarcodeScanner.AddObserver(this); //Add self to the clsBarCodeScanner

}

public void MethodWantToCall()

{

// THE METHOD I WANT TO CALL.

}

#region IObserver Members

void IObserver.Update()

{

MethodWantToCall(); //Call method when notified

}

#endregion

}

This achieves high level of decoupling and importantly,the observer itself can serve as an observable later.

Karthik Krishnaswami  Tuesday, May 23, 2006 1:36 AM
Thanks for the reply people!

Karthik Krishnaswami, that is a fantastic example! It works a treat!

I never knew this pattern existed!

Thanks

Tryst


Tryst  Tuesday, May 23, 2006 12:44 PM

this should work i think

public delegate void FormNotifyEventhandler();

// Barcode Class
class clsBarcodeScanner
{
// Declare and initialise EventsHandlers.
private System.EventHandler MyEventHandler = null;

public event FormNotifyEventhandlernotify;

private bool InitReader()
{
// Create event handler delegate
this.MyEventHandler = new EventHandler(MyReader_ReadNotify);
}

private void MyReader_ReadNotify(object sender, EventArgs e)
{
// If it is a successful read (as opposed to a failed one)
if (MyReaderData.Result == Symbol.Results.SUCCESS)
{
Notify

}
}
}

// Form Class
public class FormSnagList : System.Windows.Forms.Form
{
private clsBarcodeScanner m_clsBarcodeScanner;

public FormSnagList()
{
m_clsBarcodeScanner = new clsBarcodeScanner();

m_clsBarcodeScanner.notify += new System.EventHandler(this.MethodWantToCall);

}

public void MethodWantToCall()
{
// THE METHOD I WANT TO CALL.
}
}

Remco

RemcoJVG  Tuesday, May 23, 2006 2:19 PM

Tryst,glad to be of help!!

Can you kindly mark this as an answer?

Karthik Krishnaswami  Tuesday, May 23, 2006 2:30 PM

You can use google to search for other answers

Custom Search

More Threads

• Need help with .Net Remoting
• How to delay or disable the "End Program" dialog when intercepting the shutdown?
• SHGetFileInfo Volume Label
• Child Form Icon not Always Showing
• Question about threading Forms
• How can I read current workinbg directory of the application running?
• ShellAboutW API Call
• Location for dialog box
• WMPOCX.OCX help about URL property
• sample code for programmincally install the "correct version" of .net framework?