Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

polocar

Programmer
Sep 20, 2004
89
IT
Hi,
I’m writing a C# program (using Visual Studio 2005 Professional Edition).
I have defined a class MyPanel in the following way:

Code:
class MyPanel : Panel
{
   ...
}

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

Code:
btn.Click += new EventHandler(btn_Click);
   ...

void btn_Click(object sender, EventArgs e)
{
   ...
}

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

Code:
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

Code:
protected void InitializeControls()
{
   ...
}

in the inherited class I could write:

Code:
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:

Code:
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
 
Ok, it is sufficient to define the event handler of the base class as virtual
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top