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

Using events in a inherited form

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


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

-----------------------
Private Sub MyEventHandler(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.MyEvent
Debug.WriteLine("Bla!")
End Sub
----------------------

"Chad Miller" <ch**@predictiveconcepts.com> wrote in message
news:Gd******************@twister.tampabay.rr.com. ..
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

Nov 20 '05 #2

P: n/a
* "Chad Miller" <ch**@predictiveconcepts.com> scripsit:
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!

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
> Events are not inherited!

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

Trev.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uc****************@TK2MSFTNGP09.phx.gbl...
* "Chad Miller" <ch**@predictiveconcepts.com> scripsit:
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!

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #4

P: n/a
* "Codemonkey" <hu*********@hotmail.com> scripsit:
Events are not inherited!


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...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
> That's what I wanted to say...

I believe you ;)

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e6****************@TK2MSFTNGP12.phx.gbl...
* "Codemonkey" <hu*********@hotmail.com> scripsit:
Events are not inherited!


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...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #6

P: n/a
I want to catch an event of the base class?

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uc****************@TK2MSFTNGP09.phx.gbl...
* "Chad Miller" <ch**@predictiveconcepts.com> scripsit:
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!

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #7

P: n/a
* "Chad Miller" <ch**@predictiveconcepts.com> scripsit:
I want to catch an event of the base class?


Post your code!

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

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

"Chad Miller" <ch**@predictiveconcepts.com> wrote in message
news:Gd******************@twister.tampabay.rr.com. ..
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

Nov 20 '05 #9

P: n/a
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.
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
--
Joe Fallon

"Chad Miller" <ch**@predictiveconcepts.com> wrote in message
news:Gd******************@twister.tampabay.rr.com. ..
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


Nov 20 '05 #10

P: n/a
Thanks for the tip!
--
Joe Fallon

"Codemonkey" <hu*********@hotmail.com> wrote in message
news:uj**************@TK2MSFTNGP11.phx.gbl...
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.
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
--
Joe Fallon

"Chad Miller" <ch**@predictiveconcepts.com> wrote in message
news:Gd******************@twister.tampabay.rr.com. ..
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



Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.