About using event

  • Thread starter Thread starter tony
  • Start date Start date
T

tony

Hello!

What is the differens if I use event handler onSizeChanged compare to using
the other event handler MeltPracForm_SizeChanged.
I see both as event handler is that right?
I catch the event in both cases. So is it more or less the same thing which
of these two event handler I use.

protected override void OnSizeChanged(EventArgs e)
{
....
}


private void MeltPracForm_SizeChanged(object sender, System.EventArgs e)
{
....
}

//Tony
 
The convention I believe is that the OnXXX methods are not event
handlers but are the methods that you use to raise the events of your
classes. The other methods are what the third party actually wires up
to handle the events of your classes.
 
tony said:
What is the differens if I use event handler onSizeChanged compare to using
the other event handler MeltPracForm_SizeChanged.
I see both as event handler is that right?

No. One is a protected method, the other is a .NET event. The general
pattern for .NET components is for the base class implementation of the
protected method (OnSizeChanged in this case) to invoke the event
handler (SizeChanged in this case).
protected override void OnSizeChanged(EventArgs e)
{
...
}

This is suitable if you are creating a new control for reuse elsewhere.
For example, if you want to save this form in a class library (.DLL) and
reuse it in other applications, it may work better for you. You can even
choose not to trigger the underlying event by not calling the base class
implementation.
private void MeltPracForm_SizeChanged(object sender, System.EventArgs e)
{
...
}

This method gets inserted (via a delegate) into the SizeChanged event in
the InitializeComponent method by the VS designer. Search the
InitializeComponent method body and you'll see something like:

this.SizeChanged += new EventHandler(MeltPracForm_SizeChanged);

This creates a subscription to this event so that the given method gets
called when the event is triggered. The event is triggered by the base
class implementation of OnSizeChanged.

-- Barry
 
Hi,


tony said:
Hello!

What is the differens if I use event handler onSizeChanged compare to
using
the other event handler MeltPracForm_SizeChanged.
I see both as event handler is that right?
I catch the event in both cases. So is it more or less the same thing
which
of these two event handler I use.

You do not catch the event in both cases, with the OnXXX you handle let call
it "the window event" , the WM_XXXX event that the control receives from
windows. There is a method WndProc that receives all the events from
windows, then call predefined methods, these are the OnXXX methods you see.
In the OnXXX method you format the info received and then call the events
like SizeChanged.
 
Would it be true to say that usually the main functional difference with the
protected override method vs calling a delegate is the ability to override
the default behavor by returning true. This doesn't appear to be the case in
this example as there is a void return value, but often protected override
methods return a bool which allow you to do this, and you just can't do that
with the delegate.
 
Robert Halford said:
Would it be true to say that usually the main functional difference with the
protected override method vs calling a delegate is the ability to override
the default behavor by returning true. This doesn't appear to be the case in
this example as there is a void return value,

No. By convention, .NET events don't use return values. Instead, they
usually use a property on the event-args parameter, usually descended
from EventArgs.

Protected virtual methods can have any valid signature. However, the On*
virtual methods are designed to call the corresponding events, so to
match the event signatures, they only accept the corresponding
event-args parameter. They don't accept the 'object sender' parameter
since they supply that when invoking the event.
but often protected override
methods return a bool which allow you to do this, and you just can't do that
with the delegate.

You can return any value you like from a delegate (but multiple
subscriptions cause only one return value to be used):

---8<---
using System;

class App
{
delegate int ValueGetter();

static event ValueGetter GetValue;

static int MyGetValue()
{
return 42;
}

static void Main()
{
GetValue += MyGetValue;

Console.WriteLine(GetValue());
}
}
--->8---

-- Barry
 
Hello!!

I just wonder which of these two methods is most suitable to use in this
circumstances
when the main application window form is minimized I will minimize
all window forms that the main window form has made visible.
To make this possible I can use whichever of these two methods.

When the window form for the application is minimized this method
OnSizeChanged is called.
Protected override void OnSizeChanged(EventArgs e)
{
...
}

I have used the form designer to be able to create this event handler.
This method MeltPracForm_SizeChanged is also called when the window form
for application is minimized.
private void MeltPracForm_SizeChanged(object sender, System.EventArgs e)
{
...
}

So what method do you suggest is most suitable to use for me?

//Tony
 
Tony Johansson said:
I just wonder which of these two methods is most suitable to use in this
circumstances
when the main application window form is minimized I will minimize
all window forms that the main window form has made visible.

If you're just trying to implement an event in the user interface, I
recommend that you go with the event handler. The designer surface knows
more about it:
private void MeltPracForm_SizeChanged(object sender, System.EventArgs e)

-- Barry
 
Back
Top