By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,202 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,202 IT Pros & Developers. It's quick & easy.

About using event

P: n/a
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
May 15 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

May 15 '06 #2

P: n/a
"tony" <jo*****************@telia.com> wrote:
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
May 15 '06 #3

P: n/a
Hi,
"tony" <jo*****************@telia.com> wrote in message
news:O$**************@TK2MSFTNGP04.phx.gbl...
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.


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
May 15 '06 #4

P: n/a
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.

"tony" <jo*****************@telia.com> wrote in message
news:O$**************@TK2MSFTNGP04.phx.gbl...
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

May 15 '06 #5

P: n/a
"Robert Halford" <ro************@tiscali.com> wrote:
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
May 15 '06 #6

P: n/a
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
"Robert Halford" <ro************@tiscali.com> skrev i meddelandet
news:eY**************@TK2MSFTNGP02.phx.gbl...
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.

"tony" <jo*****************@telia.com> wrote in message
news:O$**************@TK2MSFTNGP04.phx.gbl...
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




May 15 '06 #7

P: n/a
"Tony Johansson" <jo*****************@telia.com> wrote:
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
May 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.