Using events in a inherited form

C

Chad Miller

I currently have a base form that I inherit. The base for has a custom
event. The event will not raise threw the inherited form. I was wondering
if events work threw inheritance or should I use some other method?

--
Chad Miller
President and Director of Software Development
Predictive Concepts, Inc.
www.predictiveconcepts.com
407.327.9910
 
C

Codemonkey

It seems to work fine for me (VS2003, .net 1.1).

Could you post a short code example?

If you are using VB, an example of the event handler in the derived form
would be:
 
H

Herfried K. Wagner [MVP]

* "Chad Miller said:
I currently have a base form that I inherit. The base for has a custom
event. The event will not raise threw the inherited form. I was wondering
if events work threw inheritance or should I use some other method?

Are you catching an event of the base class? Or do you want to raise an
event in the base class?

To raise an event in the base class, define a protected method
'On<EventName>' which expects the parameters of the event handler as
arguments. This method raises the event defined in the base class. The
method can be called by a derived class to raise the base class'
event. Events are not inherited!
 
C

Chad Miller

I want to catch an event of the base class?

Herfried K. Wagner said:
Are you catching an event of the base class? Or do you want to raise an
event in the base class?

To raise an event in the base class, define a protected method
'On<EventName>' which expects the parameters of the event handler as
arguments. This method raises the event defined in the base class. The
method can be called by a derived class to raise the base class'
event. Events are not inherited!
 
C

Codemonkey

Events are not inherited!

Not correct. Events *are* inherited, but a derived class cannot raise an
event defined in a base class.

Trev.
 
H

Herfried K. Wagner [MVP]

* "Codemonkey said:
Not correct. Events *are* inherited, but a derived class cannot raise an
event defined in a base class.

That's what I wanted to say...
 
J

Joe Fallon

I just went through something similar.
In the Base form I double clicked a button control and got the stub with the
event handler on the end of it.
In the child form I "saved some time" by copying the stub from the Base form
and accidentally included the event handler. Then I changed it to Overrides
instead of Overridable.
Anyway the event code ran twice! What a pill finding that one was.

Bottom line - just put the event in the Base form and override it in the
child.

e.g.
Base Form:
Protected Overridable Sub frmSelectBase_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'Override this method in child classes. Do NOT include an Event Handler
in the child class - or it will run twice!
End Sub

Child form:
Protected Overrides Sub frmSelectBase_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs)
'note the lack of event handler (Handles MyBase.Load)
'do stuff here
End Sub
 
C

Codemonkey

Joe,

This isn't the neatest way of doing it (especially with the Form.Load
Event). For example, if you need to catch the Form Load event in both the
base class and the derived class, you are relying on the derived class
calling MyBase.Form_Load.

IMHO, a better way of catching the Form.Load event in both the base class
and derived class would be:
--------------------------------
*Base Form*
Private Sub Form_Load(sender as object, e as EventArgs) handles MyBase.Load

' Do stuff to initialize base form.

End Sub

*Derived Form*
Private Sub Form_Load(sender as object, e as EventArgs) handles MyBase.Load

' Do stuff to initialize derived form.

End Sub

--------------------------------

Notice that the event handler in both forms is declared with *private*
scope. This will prevent the derived class from overriding the base classes
event handler and being called twice.

If you don't need to catch the Form.Load event in the base class, simply
omit the event handler. This way, child classes can catch the event as
normal.


Hope this helps,

Trev.
 
J

Joe Fallon

Thanks for the tip!
--
Joe Fallon



Codemonkey said:
Joe,

This isn't the neatest way of doing it (especially with the Form.Load
Event). For example, if you need to catch the Form Load event in both the
base class and the derived class, you are relying on the derived class
calling MyBase.Form_Load.

IMHO, a better way of catching the Form.Load event in both the base class
and derived class would be:
--------------------------------
*Base Form*
Private Sub Form_Load(sender as object, e as EventArgs) handles MyBase.Load

' Do stuff to initialize base form.

End Sub

*Derived Form*
Private Sub Form_Load(sender as object, e as EventArgs) handles MyBase.Load

' Do stuff to initialize derived form.

End Sub

--------------------------------

Notice that the event handler in both forms is declared with *private*
scope. This will prevent the derived class from overriding the base classes
event handler and being called twice.

If you don't need to catch the Form.Load event in the base class, simply
omit the event handler. This way, child classes can catch the event as
normal.


Hope this helps,

Trev.
 

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