如何覆盖方法?

时间:2022-09-01 14:27:33

I created an OnPaint event of my button,which I lately tried to override,but I failed

我创建了一个按钮的OnPaint事件,我最近试图覆盖它,但我失败了

My code:

    protected override void button1_Paint(PaintEventArgs e)
    {
    }

I get this error: "no suitable method found to override".

我收到此错误:“找不到合适的方法来覆盖”。

What should I do to make the error dissapear,but keep the method as override?

我应该怎么做才能使错误消失,但保持方法为覆盖?

6 个解决方案

#1


If the method is not virtual, you can't override it. If you can't override it, there is no point in trying to keep the override keyword.

如果方法不是虚拟的,则无法覆盖它。如果您无法覆盖它,那么尝试保留override关键字是没有意义的。

If you want to shadow the method, you use the new keyword instead of override.

如果要隐藏方法,可以使用new关键字而不是覆盖。

#2


The method that you want to override is probably called OnPaint, not button1_Paint. Change the method declaration so it looks like this instead:

您要覆盖的方法可能称为OnPaint,而不是button1_Paint。更改方法声明,使其看起来像这样:

protected override void OnPaint(PaintEventArgs e) { }

Note though that this code should be in a subclass of the class from which you want to override the method. If you place this method in a form, it will handle that form's painting.

请注意,此代码应位于要从中重写方法的类的子类中。如果将此方法放在表单中,它将处理该表单的绘制。

#3


In your base-class, you need to declare the method as virtual

在您的基类中,您需要将该方法声明为虚拟

example:

public class Person
{
    public virtual void DoSomething()
    {
        // do something here
    }
}

public class Employee : Person
{
    public override void DoSomething()
    {
        base.DoSomething();
    }
}

Edit:

Perhaps this can help you out?

也许这可以帮到你?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
    }

    private void button1_Paint(object sender, PaintEventArgs e)
    {

    }


}

#4


You should declare the method in the base class as virtual in order to be able to override it.

您应该将基类中的方法声明为虚拟,以便能够覆盖它。

#5


you override methods coming from other classes. To keep the method you'll have to put it in a child class and call it from there.

您覆盖来自其他类的方法。要保留该方法,您必须将其放在子类中并从那里调用它。

do you have the override in the same class as the original method? if so, just merge the functions and remove the override.

你在与原始方法相同的类中有覆盖吗?如果是这样,只需合并函数并删除覆盖。

#6


I guess you have something like this now:

我猜你现在有类似的东西:

public class MyButton : Button {
    public MyButton() {
        this.Paint += new PaintEventHandler(MyButton_Paint);
    }

    void MyButton_Paint(object sender, PaintEventArgs e) {
        //your code
    }
}

If you inherited from a button you should use this code:

如果从一个按钮继承,则应使用以下代码:

public class MyButton : Button {
    protected override void OnPaint(PaintEventArgs pevent) {
        base.OnPaint(pevent);
    }
}

#1


If the method is not virtual, you can't override it. If you can't override it, there is no point in trying to keep the override keyword.

如果方法不是虚拟的,则无法覆盖它。如果您无法覆盖它,那么尝试保留override关键字是没有意义的。

If you want to shadow the method, you use the new keyword instead of override.

如果要隐藏方法,可以使用new关键字而不是覆盖。

#2


The method that you want to override is probably called OnPaint, not button1_Paint. Change the method declaration so it looks like this instead:

您要覆盖的方法可能称为OnPaint,而不是button1_Paint。更改方法声明,使其看起来像这样:

protected override void OnPaint(PaintEventArgs e) { }

Note though that this code should be in a subclass of the class from which you want to override the method. If you place this method in a form, it will handle that form's painting.

请注意,此代码应位于要从中重写方法的类的子类中。如果将此方法放在表单中,它将处理该表单的绘制。

#3


In your base-class, you need to declare the method as virtual

在您的基类中,您需要将该方法声明为虚拟

example:

public class Person
{
    public virtual void DoSomething()
    {
        // do something here
    }
}

public class Employee : Person
{
    public override void DoSomething()
    {
        base.DoSomething();
    }
}

Edit:

Perhaps this can help you out?

也许这可以帮到你?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
    }

    private void button1_Paint(object sender, PaintEventArgs e)
    {

    }


}

#4


You should declare the method in the base class as virtual in order to be able to override it.

您应该将基类中的方法声明为虚拟,以便能够覆盖它。

#5


you override methods coming from other classes. To keep the method you'll have to put it in a child class and call it from there.

您覆盖来自其他类的方法。要保留该方法,您必须将其放在子类中并从那里调用它。

do you have the override in the same class as the original method? if so, just merge the functions and remove the override.

你在与原始方法相同的类中有覆盖吗?如果是这样,只需合并函数并删除覆盖。

#6


I guess you have something like this now:

我猜你现在有类似的东西:

public class MyButton : Button {
    public MyButton() {
        this.Paint += new PaintEventHandler(MyButton_Paint);
    }

    void MyButton_Paint(object sender, PaintEventArgs e) {
        //your code
    }
}

If you inherited from a button you should use this code:

如果从一个按钮继承,则应使用以下代码:

public class MyButton : Button {
    protected override void OnPaint(PaintEventArgs pevent) {
        base.OnPaint(pevent);
    }
}