Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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.

like image 581
nik Avatar asked Jul 02 '10 11:07

nik


1 Answers

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.

like image 172
Quartermeister Avatar answered Nov 04 '22 18:11

Quartermeister