473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using events in a inherited form

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
10 2110
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
* "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
> 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
* "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
> 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
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
* "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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
2
by: Andrew | last post by:
Hi, friends, I need to raise certain events in my VC# windows control library. Any reference paper or sample source code for help? Thanks a lot...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
2
by: DotNetShadow | last post by:
Hi Guys, I'm trying to work out how events work in VB.NET Basically I want to create a base class that has an Event. I would like all derived classes to inherit this event. I sorta worked out...
2
by: Mark | last post by:
Hi all I just started usuing vb2005 after many years on vb6. I had a go at creating a custom control. I added several events to my control such as 'NewClientAdded' and placed it on a test...
2
by: MuZZy | last post by:
Hi, Consider i have this code: // --------------------------------------------------------------------- public delegate void DoSearchEventHandler(Form f, DoSearchEventArgs e); public class...
7
by: Jean Paul Mertens | last post by:
Hello, Is there a way to send a string of text to a generic tekst printer under ..NET. Somethings as in the good old days File f = Open("LPT1"); f.Writeline("Blablabla"); The goal is to use...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
0
by: DaveL | last post by:
I have a Base Window public class InheritForm:BaseForm when Controls are added i want to add a eventhandler that will fire 1st before the other control events as in the leave and enter events...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.