Scope of delegates/event handlers

G

Guest

Platform: WinCE 4.2, C#

I am working on a kiosk application where I have a System.Windows.Forms.Form
that contains a System.Windows.Forms.Panel derived class named HostPanel.
This panel in turn contains a System.Windows.Forms.Panel derived class named
ChildPanel. (For this example I have simplified the naming)
What I want to do is have the ChildPanel fire an event - when it has
completed its work - that the parent, HostPanel will catch and handle.
I am having trouble getting it to compile. I get the following compiler error:
"The event 'HostPanel.ChildButtonEvent' can only appear on the left hand
side of += or -= (except when used from within the type 'ChildPanel')"

Here is a simplified example of the code I am using:

class HostPanel : Panel
{
public delegate void ChildDoneEventHandler(object sender, EventArgs e);
public event ChildDoneEventHandler ChildDoneEvent;

HostPanel host;
Button btn;
ChildPanel child;

HostPanel()
{

this.ChildDoneEvent += new
ChildDoneEventHandler(HostPanel_ChildDoneEvent);

}


private void HostPanel_ChildDoneEvent(object sender, EventArgs e)
{
// Handle the child panel's event here.
}


class ChildPanel : Panel
{
HostPanel host;
Button btn;

ChildPanel(HostPanel pnl)
{
this.host = pnl;
this.Parent = pnl;

btn = new Button();
/// set all button properties.
btn.Click += new new System.EventHandler(this.btn_Click);
}


private void btn_Click(object sender, System.EventArgs e)
{
/// ERROR here:
/// "The event 'HostPanel.ChildButtonEvent' can only appear on the left
hand side of += or -= (except when used from within the type 'ChildPanel')"

this.host.ButtonEvent(sender,e);
}

}

I tried to declare the delegate and event outside of both classed but the
compiler will not allow events declared outside of classes.

Any ideas would be greatly appreciated.

TIA,
Mark
 
G

Guest

The delegate should be declared at namespace level (outside the classes).
The event in the object that exposes it, not the consumer. If you want the
panel to expose events of the child, then you must have an event in the
child that the Panel gets, and then re-fires through it's own event.

-Chris
 
G

Guest

Hi Mark,
I get a different error when trying to compile your code at the following
line:

this.host.ButtonEvent(sender,e);

this is the error:
HostPanel does not contain a definition for ButtonEvent

I assumed this was a typo and replace the ButtonEvent with ChildDoneEvent
and the code compiled with no errors.
I am using Visual Studio 2003 (.Net Framework 1.1)

Let me know if my substitution was right, if not please send the actual code
you are using.

Thanks,
Yves.
 
G

Guest

Yes, that is a typo. The line should be

this.host.ChildDoneEvent(sender,e);

I am going to declare the delegate at namespace scope and the event in the
child panel as Chris suggested.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top