Winforms事件,'找不到合适的方法来覆盖' [英] Winforms events, 'no suitable method found to override'

查看:55
本文介绍了Winforms事件,'找不到合适的方法来覆盖'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在重叠事件中包含(对象发件人)时,有没有人知道为什么我一直得到"没有找到合适的方法来覆盖"错误?

does anybody know why I keep getting the 'no suitable method found to override' error when I include (object sender) in overrided events?

我认为这是因为我没有声明base()构造函数,但那不是它。

I thought that it was because I didn't declare the base() constructor, but that's not it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace MyFirstTileEditor.Controls
{
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
    public class HexBox : TextBox
    {

        public HexBox() : base()
        {

        }

        /////////////////////////////////////////////////////////////////////////////////
        //Events
        /////////////////////////////////////////////////////////////////////////////////

        protected void OnTextChanged(object sender, EventArgs e)
        {
            string text = Text;
            int n = 0;

            if (!int.TryParse(text, System.Globalization.NumberStyles.HexNumber, System.Globalization.NumberFormatInfo.CurrentInfo, out n) && text != String.Empty)
            {
                Text = text.Remove(text.Length - 1, 1);
                SelectionStart = Text.Length;
            }
        }
        protected override void OnResize(object sender, EventArgs e)
        {

        }
    }
}

编辑:

好吧我觉得我知道了我不能使用覆盖关键字,然后我只是设置我正在做的事件..

ok I think I got it I can't use the override keyword and then I just setup the event my doing..

        public HexBox() : base()
        {
            base.TextChanged += OnTextChanged;
        }




推荐答案

您好Shadowblitz16,

Hi Shadowblitz16,

对不起我迟到的回复。

>>'找不到合适的方法来覆盖'

>>'no suitable method found to override'

需要使用override修饰符来扩展或修改继承的方法,属性,索引器或事件的抽象或虚拟实现。您还可以从

此文档。
 这意味着方法(或事件) 您想要覆盖的内容必须存在于基类中且参数是一致的。

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event. You can also find this information from this document. That means the method(or event) what you want to override must exist in the base class and the parameters are consistent.

但是,当我们通过按F12查看TextBox类(它由HexBox类继承)时,  ;有   事件名为OnResize,因此您无法覆盖它。

However, when we view the TextBox class(it is inherited by HexBox class) through press F12, there is not a event named OnResize, so you can not override it.

作为替代方案,我建议你可以创建一个没有override关键字的新事件(带有新参数),它可以正常工作。

As an alternative, I suggest you can create a new event (with the new parameter) without the override keyword and it will work just fine.

希望它有所帮助!

最诚挚的问候,

Stanly


这篇关于Winforms事件,'找不到合适的方法来覆盖'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆