473,756 Members | 5,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to grow rich in C# the event handler of a class in an inherited class

Hi,
I'm writing a C# program (using Visual Studio 2005 Professional
Edition).
I have defined a class MyPanel in the following way:

class MyPanel : Panel
{
...
}

In this class I have put a btn Button control, and I have defined the
event handler:

btn.Click += new EventHandler(bt n_Click);

...

void btn_Click(objec t sender, EventArgs e)
{
...
}

Then, I have defined another class, inherited from MyPanel:

class MyPanel2 : MyPanel

In this class I would like to enrich the btn_Click event handler (the
code of the base class should be executed anyway), but I don't know
why...
If it were a method, as for example

protected void InitializeContr ols()
{
...
}

in the inherited class I could write:

protected override void InitializeContr ols()
{
base.Initialize Controls();
...
}

but if I want to enrich an event handler and not a method, how can I
do? (I have tried to add the "protected" keyword in the event
handler definition of MyPanel base class:

protected void btn_Click(objec t sender, EventArgs e)
{
...
}

but when I go in the inherited class and try to write "protected
override", the methods list the compiler shows me doesn't include
this event handler...

Can you give me any suggestion about that?
Thank you very much

Dec 11 '06 #1
3 1816
to override, it must first be virtual (e.g. "protected virtual
SomeMethod(blah )"). For use in inheritance, I would also personally
tweak the name to be a bit clearer too.

Marc
Dec 11 '06 #2
If you create a method OnButtonClick and make it overridable, then in
the derived class it will be called when ever the event fires in the
base class.

So your base class will wire up the event for the button to fire and
call

protected virtual void OnButtonClick(S ystem.EventArgs e)

The derived class will have
protected override void OnButtonClick(S ystem.EventArgs e)
When the button in the base class is clicked the override version in
the derived class will be called.
If you want to do something in addition to what the base class usually
does call

base.OnButtonCl ick (e);
after doing your processing in the derived class.

polocar wrote:
Hi,
I'm writing a C# program (using Visual Studio 2005 Professional
Edition).
I have defined a class MyPanel in the following way:

class MyPanel : Panel
{
...
}

In this class I have put a btn Button control, and I have defined the
event handler:

btn.Click += new EventHandler(bt n_Click);

...

void btn_Click(objec t sender, EventArgs e)
{
...
}

Then, I have defined another class, inherited from MyPanel:

class MyPanel2 : MyPanel

In this class I would like to enrich the btn_Click event handler (the
code of the base class should be executed anyway), but I don't know
why...
If it were a method, as for example

protected void InitializeContr ols()
{
...
}

in the inherited class I could write:

protected override void InitializeContr ols()
{
base.Initialize Controls();
...
}

but if I want to enrich an event handler and not a method, how can I
do? (I have tried to add the "protected" keyword in the event
handler definition of MyPanel base class:

protected void btn_Click(objec t sender, EventArgs e)
{
...
}

but when I go in the inherited class and try to write "protected
override", the methods list the compiler shows me doesn't include
this event handler...

Can you give me any suggestion about that?
Thank you very much
Dec 11 '06 #3
ok guys, thank you very much

Dec 11 '06 #4

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

Similar topics

3
4469
by: Steve Long | last post by:
Hello, I have a VB.NET class that raises a MapSet event that passes an argument of type interop.MapObjects2.MapClass. I have a C# class that inherits from this VB.NET class. How can I handle the VB.NET event in the C# class? In VB.NET, you would just write: Handles MyBase.MapSet I would appreciate any help on this.
1
3355
by: joe | last post by:
Happy New Year to you all and may the source be with you! I have a class, which handles an event and I derive from this class. I dont want the event handler in the base class to be called anymore as it does things I dont want it to do. I want to handle the event in the derived class. How do I do it? One way is to declare the event handler in the base class as protected virtual. Is there another way?
5
5053
by: Sergey Atun | last post by:
Hi, I inherited form and I writed it's load event: public class frmBase: System.Windows.Forms.Form { .... private void frmBase_Load(object sender, System.EventArgs e) { MessageBox.Show("this event must run after derived class' (frmInherited) load event");
1
6958
by: WFB | last post by:
Hi, I have a base class from which all of my pages derive (ABCBasePage). For example, ABCCustomerSelect Inherits ABCPasePage. I would now like to have ABCPocketSelect which should inherit from ABCCustomerSelect. My problem is that when ABCPocketSelect is loaded the Page_Load event in ABCBasePage is called, followed by the load event for ABCCustomerSelect - and I would like to skip the ABCCustomerSelect load event.. My basic code is...
5
6174
by: J McD | last post by:
Hi I have a DataGrid with an ImageButton column. When I click on an imagebutton I get a postback but it doesn't run the OnImgBtnClick method. I can actually comment out the line where I add this Click event to the ImageButton Column and it makes no difference, I still get a postback. This is driving me crazy...something seems to be causing an OnClick postback and it isn't my Click event I've included the code below....any help will be...
15
26513
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
4
2653
by: JC Voon | last post by:
Hi: My base form has a button, when click it will call MessageBox.Show( "Base form" ). I inherite a child form from the base and assign a button click event to the same button which will call MessageBox.Show( "Inherited form" ). I run my application, and load the clild form, click the button it show both message !!! Is there any way to control the event sequence so that it only show the child form event, or i can control whether to...
9
2471
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the post event will always execuate after the system is completed... - I want to wrap this functionality in a framework, so I could possibly have 3 or 4 levels of inherited objects that need to have these pre / post events executed before and after the...
3
2013
by: Matt F. | last post by:
I have an abstract class that about a dozen sub-classes inherit from. I want to enforce that each sub-class shadows an event in the abstract class, but can't quite figure out how to do this. Basically, all of the inherited classes deal with data in some way or another. I need to make sure that when the data is changed on any of those classes, they raise an event called "DataChanged", and have that enforced in some way from the abstract...
0
9487
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
9297
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
10069
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
9904
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...
1
9884
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8736
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...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
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...
3
2697
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.