473,837 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Events vs Overrides

In a form, there is a set of events that can be handled. There is also a
list of (Overrides). So, for the Closed event, for example, there is an
override OnClosed.
Where should exit code be placed: in the Closed event handler, or in the
OnClosed override function?

What is the difference, and why have the two? When would I use each one?
Probably quite basic questions, but it has been puzzling me. The answers to
these questions would also indicate how my own classes should be
constructed.

TIA

Charles
Nov 20 '05 #1
4 1352
Charles,
Where should exit code be placed: in the Closed event handler, or in the
OnClosed override function? For classes that are derived from Form, I would put the exit code in the
overridden OnClosed method, remembering to call MyBase.OnClosed .

For objects that have a reference to the Form, I would put the exit code in
the Closed event handler code in that object.

In other words the Event is for classes not derived from the base class,
while the OnClosed is for derived classes. OnClosed also allows derived
classes to raise the event, or to supplement what happens when the event is
raised (pre or post processing).

For more details see:

http://msdn.microsoft.com/library/de...Guidelines.asp

http://msdn.microsoft.com/library/de...Guidelines.asp

Note VB.NET's RaiseEvent itself will check for handlers attached to the
event, you can simply call RaiseEvent directly, you don't need to check for
Nothing as the above pages show.

Something like:

Public Class Class1

Private WithEvents m_base As Base

Public Sub New()
m_base = New Derived
End Sub

Private Sub Base_Changed(By Val sender As Object, ByVal e As
System.EventArg s) Handles m_base.Changed
' An aspect of the Base variable changed.
End Sub

End Class

Public Class Base

Public Event Changed As EventHandler

Private m_name As String

Protected Overridable Sub OnChanged(ByVal e As EventArgs)
RaiseEvent Changed(Me, e)
End Sub

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
OnChanged(Event Args.Empty)
End Set
End Property

End Class

Public Class Derived
Inherits Base

Private m_value As String

Public Property Value() As String
Get
Return m_value
End Get
Set(ByVal value As String)
m_value = value
OnChanged(Event Args.Empty)
End Set
End Property

Protected Overrides Sub OnChanged(ByVal e As System.EventArg s)
' The Derived class wants to know if something changed
' In this example both Base & Derived raise this event!

' Do some thing before the event itself is raised
MyBase.OnChange d(e)
' Do some thing after the event itself is raised

End Sub

End Class

Hope this helps
Jay

"Charles Law" <bl***@nowhere. com> wrote in message
news:Oh******** ******@TK2MSFTN GP12.phx.gbl... In a form, there is a set of events that can be handled. There is also a
list of (Overrides). So, for the Closed event, for example, there is an
override OnClosed.
Where should exit code be placed: in the Closed event handler, or in the
OnClosed override function?

What is the difference, and why have the two? When would I use each one?
Probably quite basic questions, but it has been puzzling me. The answers to these questions would also indicate how my own classes should be
constructed.

TIA

Charles

Nov 20 '05 #2
Hi Jay

The kind of response I would have expected from you. A good, solid response.
Thank you.

When calling MyBase.OnClosed , is the recommendation to call it at the start
of the OnClosed method or at the end?

Cheers.

Charles

[I have deleted the rest of the message as I have discovered that Outlook
Express fails to send when I include the remains of previous messages. Any
thoughts?]
Nov 20 '05 #3
Charles,
When calling MyBase.OnClosed , is the recommendation to call it at the start of the OnClosed method or at the end? I normally put it at the beginning, as it doesn't matter to me if the base
event is raised before or after my code. So I let the event be raised first.

If I want or need my code to execute before the base event is raised, I put
MyBase.OnClosed at the end.

I was trying to show you in the example, that sometimes you may want to put
MyBase.OnClosed in the middle, as you want to do some pre-processing &
post-processing when the base event is raised.

Hope this helps
Jay

"Charles Law" <bl***@nowhere. com> wrote in message
news:Oe******** ******@TK2MSFTN GP12.phx.gbl... Hi Jay

The kind of response I would have expected from you. A good, solid response. Thank you.

When calling MyBase.OnClosed , is the recommendation to call it at the start of the OnClosed method or at the end?

Cheers.

Charles

[I have deleted the rest of the message as I have discovered that Outlook
Express fails to send when I include the remains of previous messages. Any
thoughts?]

Nov 20 '05 #4
Thanks again Jay.

Charles
Nov 20 '05 #5

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

Similar topics

5
1697
by: ECVerify.com | last post by:
This should be a basic question. In VB.NET in the two drop downs over the source code for a form you can get a list of the events and overrides for that form. In VC++ in the properties window you can get the events and overrides. But for some reason I can not seem to find the list of events and overrides for a C# form, if I do this. the Intelisense will show them but as for being able to select it and have the IDE pop the function or...
4
1680
by: James Hancock | last post by:
I have a page, which is inherited from another page (PageEx is what I call it) All of our pages are based on PageEx because there is a bunch of logic stuff we do in there. My problem is, that if I have a page that is inherited from PageEx, the post back events don't get called on the page. If I put back the page to inherit from UI.Page everything is fine except we don't have database settings etc. Anyone have any ideas? Thanks!
10
2150
by: Chad Miller | last post by:
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
1
888
by: Lee Gillie | last post by:
..NET Events and class method overrides through inheritance provide similar functionality for class consumers. Does anyone know (for sure) which performs better? How much better? Also interested in all opinions about the strategy of choosing one over the other? Perhaps general or specific situations where one leaves more options open? TIA for any perspective you can provide based on your experience. Best regards - Lee Gillie, Spokane WA
4
1410
by: Bill English | last post by:
How do I assign a method to the click event of a specific toolbar button? When I look under ToolbarButton1 events, all I see is disposed.
3
3219
by: Lance | last post by:
I've noticed that controls that are contained in MDI child forms fail to raise MouseLeave events if the MDI child form's MdiParent property is set to Nothing (after it was set to an existing MDI container form) or if the MDI child form's Visible property is set to False (after the MDI child form was shown). This is an enormous problem for my app because I must show different MDI child forms based on the state of my application and many of the...
2
1313
by: Dennis | last post by:
I have a control that inheirts from "Panel". I want to prevent the user from having access to the MoueUp event. I tried making a sub in my control that handles MyBase.MouseUp but still this event shows up as an event the user can use. Is there anyway to prevent users of this control from getting access to events such as MouseUp. Thanks. -- Dennis in Houston
10
3135
by: Dennis | last post by:
I have a simple form with one button and one text box. In the Form, I create an array list to track the events by adding a descriptive string item to the arraylist in each event. I first Click on the Button then Click on the TextBox and enter an "a" then click on the Button again. The following is what I get for the event tracking Arraylist: but - GotFocus but - Clicked but - LostFocus txt - Changed
2
1628
by: Bert Szoghy | last post by:
Hello, I am missing something about Visual Basic .NET module variables and window close events. In the following code, after opening Form2 by clicking a button on Form1 and then closing Form2, I would expect to click on the second Form1 button and get intMyValue = 0. Form2 has code to reinitialize it in its close event, but this doesn't seem
0
9843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9683
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
10882
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
10577
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7005
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
5670
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
5851
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4476
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
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.