473,795 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2146
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.EventArg s) Handles MyBase.MyEvent
Debug.WriteLine ("Bla!")
End Sub
----------------------

"Chad Miller" <ch**@predictiv econcepts.com> wrote in message
news:Gd******** **********@twis ter.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**@predictiv econcepts.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******** ********@TK2MSF TNGP09.phx.gbl. ..
* "Chad Miller" <ch**@predictiv econcepts.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*********@ho tmail.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******** ********@TK2MSF TNGP12.phx.gbl. ..
* "Codemonkey " <hu*********@ho tmail.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******** ********@TK2MSF TNGP09.phx.gbl. ..
* "Chad Miller" <ch**@predictiv econcepts.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**@predictiv econcepts.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_L oad(ByVal sender As System.Object,
ByVal e As System.EventArg s) 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_L oad(ByVal sender As System.Object,
ByVal e As System.EventArg s)
'note the lack of event handler (Handles MyBase.Load)
'do stuff here
End Sub
--
Joe Fallon

"Chad Miller" <ch**@predictiv econcepts.com> wrote in message
news:Gd******** **********@twis ter.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_Loa d.

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(sende r as object, e as EventArgs) handles MyBase.Load

' Do stuff to initialize base form.

End Sub

*Derived Form*
Private Sub Form_Load(sende r 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******@nospa mtwcny.rr.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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_L oad(ByVal sender As System.Object, ByVal e As System.EventArg s) 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_L oad(ByVal sender As System.Object,
ByVal e As System.EventArg s)
'note the lack of event handler (Handles MyBase.Load)
'do stuff here
End Sub
--
Joe Fallon

"Chad Miller" <ch**@predictiv econcepts.com> wrote in message
news:Gd******** **********@twis ter.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2267
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
1917
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
6717
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 contains a few controls anchored to the sides of the form. "frmChild" inherits from "frmBase"... and the controls appear on the inherited form as expected. However things start going wrong when I place addition controls on the "frmChild" form. When I...
2
3971
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 how to do that but the real problem I have is that If I have a base class with an event and derived class 1 and 2 inherit this event. Say derived class 1 creates a new derived class 2 how does this new class 2 get the same event as derived class 1...
2
1093
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 form. What I'd like to know is how to get the events that I created to appear in the list of events on the host form ( for that control)
2
1393
by: MuZZy | last post by:
Hi, Consider i have this code: // --------------------------------------------------------------------- public delegate void DoSearchEventHandler(Form f, DoSearchEventArgs e); public class DoSearchEventArgs { public String m_sIDColumn; public String m_sQuery; public DoSearchEventArgs(String sQuery, String sIDColumn)
7
2050
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 an old lineprinter as a log printer printing out each line (incomming allert) at a time without having to build a whole page
53
4758
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, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating records that have a '99' in them (it is similar to a CSV file). Some of my concerns are:
0
902
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 ive tried Control_added event but my events fire after the inherited Controls enter/leave events
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.