473,397 Members | 2,084 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,397 software developers and data experts.

Event handling question

Hi,

I'm a newbie and trying to understand event handling in c#. I have
understood handling events using delelgate objects. But not this
method- "Event handling by overriding the virtual protected method of
the base class". Can someone please explain this with a sample code?

Thanks a lot.

Nov 17 '05 #1
4 1725
This is not really accurate. For classes that fire events, (such as the
Button class with the Click event), there is usually a method (named
On<Event>, in this case, OnClick) that is called to fire the events. This
method is usually marked as virtual, so that it can be overriden by classes
that derive from it. It is easier to put some specific logic in here than
attach a delegate to yourself (and it's quicker as well).

Usually, you will see this in classes overriding controls to paint
themselves, so they will override OnPaint.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"hillcountry74" <sh*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi,

I'm a newbie and trying to understand event handling in c#. I have
understood handling events using delelgate objects. But not this
method- "Event handling by overriding the virtual protected method of
the base class". Can someone please explain this with a sample code?

Thanks a lot.

Nov 17 '05 #2
I'm from a VB background and I guess this is not making it clear for me
to understand. What I'm still missing is that if the derived class adds
some code in the OnClick method, the base class' method must still be
called, ie base.OnClick..

In the OnClick method of the base class, the event is raised. But in
the derived class's OnClick method you are not exactlly handling the
event right? You are still raising the event. I've read in a few
articles and a book wherein they state that the derived class is
handling the event by overriding the base class method. This is still
not clear to me...

Please explain.

Thanks for your help.

Nov 17 '05 #3
Basically, when a button is clicked, the OnClick method is called to
fire the event. Now, because you are deriving from Button, you can place
some extra code in here before or after (or both) the event is fired (by
calling the base implementation) to implement some specific logic to your
class (whatever you wish it to be). This is handling the click event still,
but is only possible for those classes that offer this kind of method that
performs the default action.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"hillcountry74" <sh*******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I'm from a VB background and I guess this is not making it clear for me
to understand. What I'm still missing is that if the derived class adds
some code in the OnClick method, the base class' method must still be
called, ie base.OnClick..

In the OnClick method of the base class, the event is raised. But in
the derived class's OnClick method you are not exactlly handling the
event right? You are still raising the event. I've read in a few
articles and a book wherein they state that the derived class is
handling the event by overriding the base class method. This is still
not clear to me...

Please explain.

Thanks for your help.

Nov 17 '05 #4
Thanks a lot, Nicholas. Now it's crystal clear...:)

Nov 17 '05 #5

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

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
2
by: Marinos Christoforou | last post by:
Sorry if this has been asked before but as an inexperienced wanna-be C# programmer I wondering how to code classes to help build a standard Windows UI. For example to build a common toolbar. I...
7
by: Brian Keating | last post by:
hi there this may seem like a silly question but what exactly is the difference ... ok .. that question is probably a bit ambiguous.. i know what delegates are i know what events are, i use them...
2
by: Wavemaker | last post by:
The canonical way of declaring delegates for events is to include a parameter representing the sender as well as an EventArgs derived class (or EventArgs itself) as the second parameter...
9
by: Sridhar | last post by:
Hi, I have created a web page which includes a place holder. I also have a dropdown list in that webpage. when I select one of the choices in that dropdown list, It will load a user control...
4
by: reggiestyles | last post by:
Hi, I've got a question about prototype and event handling. I've got several div's (dynamic number) on a page that I want to set as active or inactive (basically, I'm using scriptaculous'...
3
by: a | last post by:
Hi, I have a 3-column listview. It has to return the value of the cell when the user click the listview. The SelectedIndexChanged event and ItemSelectionChanged Event only return the row...
1
by: stmfc | last post by:
hi, for an event handling mechanism, we need an event object. (the object where the event actually occur) and we need an event handler, and we need a registration of the event handler to the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.