473,385 Members | 1,474 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,385 software developers and data experts.

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(btn_Click);

...

void btn_Click(object 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 InitializeControls()
{
...
}

in the inherited class I could write:

protected override void InitializeControls()
{
base.InitializeControls();
...
}

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(object 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 1794
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(System.EventArgs e)

The derived class will have
protected override void OnButtonClick(System.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.OnButtonClick (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(btn_Click);

...

void btn_Click(object 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 InitializeControls()
{
...
}

in the inherited class I could write:

protected override void InitializeControls()
{
base.InitializeControls();
...
}

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(object 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
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...
1
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...
5
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...
1
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...
5
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...
15
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
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...
9
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...
3
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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.