delegates and Events - Newb Help

J

JaD

I am trying to understand events and delegates which is confusing me.

Okay I have a user control that derives from a GroupBox called (MyPanel)

I added this:
public delegate void Collapsed();
public event Collapsed OnCollapsed;

public void CollapseIt()
{
if (OnCollapsed!=null)
{
this.Height = this.Height - 10;
}
}

If someone calls CollapseIt() from a form (i.e. on the click event of a
button) the height of the GroupBox decreases correctly...
i.e:
private void button1_Click(object sender, System.EventArgs e)
{
c.CollapseIt();
}

I add "MyPanel" at run time and attach the Handler

private void Form1_Load(object sender, System.EventArgs e)
{
c = new MyPanel();
c.OnCollapsed+=new WindowsApplication2.MyPanel.Collapsed(c_OnCollapsed);
this.Controls.Add(c);
}
private void c_OnCollapsed()
{
MessageBox.Show("Collapsed Fired!") //<------------- THIS NEVER
FIRES!!!!!!! ? WHY?
}

Why does my MessageBox not show even if I called CollapseIt?
 
T

Tom Porterfield

I am trying to understand events and delegates which is confusing me.

Okay I have a user control that derives from a GroupBox called (MyPanel)

I added this:
public delegate void Collapsed();
public event Collapsed OnCollapsed;

public void CollapseIt()
{
if (OnCollapsed!=null)
{
this.Height = this.Height - 10;
}
}

If someone calls CollapseIt() from a form (i.e. on the click event of a
button) the height of the GroupBox decreases correctly...
i.e:
private void button1_Click(object sender, System.EventArgs e)
{
c.CollapseIt();
}

I add "MyPanel" at run time and attach the Handler

private void Form1_Load(object sender, System.EventArgs e)
{
c = new MyPanel();
c.OnCollapsed+=new WindowsApplication2.MyPanel.Collapsed(c_OnCollapsed);
this.Controls.Add(c);
}
private void c_OnCollapsed()
{
MessageBox.Show("Collapsed Fired!") //<------------- THIS NEVER
FIRES!!!!!!! ? WHY?
}

Why does my MessageBox not show even if I called CollapseIt?

Because you are never firing the event. You have created a delegate and an
event, and you even check to see if someone has added an event handler, so
now you just need to fire the event. I would change the CollapseIt method
as follows:

public void CollapseIt()
{
this.Height = this.Height - 10;
if (OnCollapsed!=null)
{
OnCollapsed(); //this will fire the event handler.
}
}

This change means that the control will collapse in all situations, and if
an event handler has been added for the OnCollapsed event.
 
B

Bruce Wood

Just a note about conventions in event naming.

Don't call your events "On...", such as "OnCollapsed". You should call
it "Collapsed" or "Collapsing". Now, an event can, syntactically, have
any name you like, but there are some conventions within .NET to which
you might want to adhere:

1. Use the past tense for events that fire after the thing has taken
place. For example, the "Collapsed" event would happen after the
panel's height changed, just as Tom wrote it above.

2. Use the gerund for events that fire before the thing has taken
place. For example, you could also (or instead) create an event called
"Collapsing" that would happen just before the panel's height changed.

3. Use "On" plus the event name to write a _protected virtual void_
method that checks the event queue and fires the event if there are any
listeners:

protected virtual void OnCollapsed()
{
if (Collapsed != null)
{
Collapsed(this, System.EventArgs.Empty);
}
}

this method is useful because in here you can put any code that should
happen just after the panel is collapsed, any child classes inheriting
from your class can override it to do the same, and you don't have to
put "if (Collapsed != null)" tests everwhere you want to fire the
event.

If you look at the .NET Framework classes they all use this naming
convention and the convention of having a protected virtual method for
every event so that child classes can intercept it.
 
T

Tom Porterfield

2. Use the gerund for events that fire before the thing has taken
place. For example, you could also (or instead) create an event called
"Collapsing" that would happen just before the panel's height changed.

Yes, all good suggestions. In addition, on this one you might want to
accept as input a CancelEventArgs object, allowing someone to cancel the
event if necessary. See the common Validating and Validated events found
in System.Windows.Forms.Control as example.

Typically as well, especially for events on Controls, the sender is passed
in as input, allowing a handler to know exactly who fired the event. The
sender would be the control firing the event.
 

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