tags:

views:

44

answers:

1

I have a code:

public class Layout : UserControl
{    
    protected void DisplayX_DisplayClicked(object sender, DisplayEventArgs e)
    {
        CurrentDisplay = (CameraPanel)sender;
    }
}

'Layout' is a base class for my other layouts. For example, I have a 'Layout1' derived from base class 'Layout'. Layout1 has an element Display01. Display01 has an DisplayClicked event. I'm trying to assign DisplayX_DisplayClicked via Visual Studio Designer to DisplayClicked event of Display01.

public partial class Layout1 : Layout
{
    private CameraPanel Display01;
}

It gives me an error:

The method 'xxx' cannot be the method for an event because a class this class derives from already defines the method.

How to use method from base class as a eventhandler of derived class ? Is it possible ? If so, how. If no, why.

A: 

The designer can't handle that, but you can do it in code just fine. In the constructor for Layout1, just write:

public Layout1()
{   
    InitializeComponent();
    this.Display01.DisplayClicked += base.DisplayX_DisplayClicked;
}

Alternately, you could let the designer generate a method named Display01_DisplayClicked, and then implement it as:

private void Display01_DisplayClicked(object sender, DisplayEventArgs e)
{
    base.DisplayX_DisplayClicked(sender, e);
}

That's a lot more verbose, so I would do it the first way, but it would let the designer be aware that there was a handler for that event.

Quartermeister
Why don't Visual Studio Designer allow to use base class method ?
nik
because designer search only in the current class.
serhio
@Quartermeister, I'll use your solution with base.xxx ... however, it's not very good, because i can't see these handlers in visual studio designer. If another programmer will look at a usercontrol in visual studio desinger => he wouldn't see nothing (anything?).@serhio, bad studia. but at the other hand, should vs designer look at the whole hierarchy ? maybe should, and maybe shouldn't ...
nik