如何将事件处理程序添加到继承的winform控件?

时间:2022-01-27 19:35:08

I need to add an event handler to an inherited control like a datagridview but Visual Studio doesn't allow me. Isn't there a way for an inherited control to fire a base event handler AND the inherited one? in a sequence I specify?

我需要将事件处理程序添加到继承的控件,如datagridview,但Visual Studio不允许我。是否有一种方法可以让继承的控件触发基本事件处理程序和继承的处理程序?按照我指定的顺序?

2 个解决方案

#1


Your question is unclear. Assuming that you're trying to handle the base class' event in the inherited control, you can override the OnEventName protected virtual method. In the override, make sure to call the base method or the event won't fire.

你的问题不清楚。假设您正在尝试在继承的控件中处理基类'事件,您可以覆盖OnEventName受保护的虚拟方法。在覆盖中,确保调用基本方法或不会触发事件。

This method exists (AFAIK) for every event in every control in System.Windows.Forms. If the control you're inheriting does not have this virtual method, you can subscribe to the vent in the constructor.

对于System.Windows.Forms中的每个控件中的每个事件,此方法都存在(AFAIK)。如果您继承的控件没有此虚方法,则可以在构造函数中订阅vent。

For example:

class MyButton : Button {
    //First way
    protected override void OnClick(EventArgs e) { 
        base.OnClick(e);    //Without this line, the event won't be fired
        //...
    }


    //Second way
    public MyButton() {
        base.Click += Base_Click;
    }

    void Base_Click(object sender, EventArgs e) {
        //...
    }
}

EDIT:

If you're trying to raise the base class' event, you can call these OnEventName methods.

如果您尝试引发基类'事件,则可以调用这些OnEventName方法。

If the base class doesn't have one, the only way to do it is to declare a new event, and add a handler to the original event from the base class that raises your new event. Note that if you have other code that uses the base class' event, and the evnet is not virtual and doesn't have a raiser method, you're out of luck, unless you can decompile the base clas and find out where the event is raised.

如果基类没有基类,唯一的方法是声明一个新事件,并从引发新事件的基类向原始事件添加一个处理程序。请注意,如果您有其他代码使用基类'事件,并且evnet不是虚拟的并且没有提升方法,那么您运气不好,除非您可以反编译基类并找出事件的位置被提出来了。

#2


Events in WinForms typically have a corresponding "On*" method. Simply call that method, and it'll raise the event. If you want to raise "CellClick" for example, call "base.OnCellClick(new DataGridViewCellEventArgs(row, column))".

WinForms中的事件通常具有相应的“On *”方法。只需调用该方法,它就会引发事件。例如,如果要引发“CellClick”,请调用“base.OnCellClick(new DataGridViewCellEventArgs(row,column))”。

#1


Your question is unclear. Assuming that you're trying to handle the base class' event in the inherited control, you can override the OnEventName protected virtual method. In the override, make sure to call the base method or the event won't fire.

你的问题不清楚。假设您正在尝试在继承的控件中处理基类'事件,您可以覆盖OnEventName受保护的虚拟方法。在覆盖中,确保调用基本方法或不会触发事件。

This method exists (AFAIK) for every event in every control in System.Windows.Forms. If the control you're inheriting does not have this virtual method, you can subscribe to the vent in the constructor.

对于System.Windows.Forms中的每个控件中的每个事件,此方法都存在(AFAIK)。如果您继承的控件没有此虚方法,则可以在构造函数中订阅vent。

For example:

class MyButton : Button {
    //First way
    protected override void OnClick(EventArgs e) { 
        base.OnClick(e);    //Without this line, the event won't be fired
        //...
    }


    //Second way
    public MyButton() {
        base.Click += Base_Click;
    }

    void Base_Click(object sender, EventArgs e) {
        //...
    }
}

EDIT:

If you're trying to raise the base class' event, you can call these OnEventName methods.

如果您尝试引发基类'事件,则可以调用这些OnEventName方法。

If the base class doesn't have one, the only way to do it is to declare a new event, and add a handler to the original event from the base class that raises your new event. Note that if you have other code that uses the base class' event, and the evnet is not virtual and doesn't have a raiser method, you're out of luck, unless you can decompile the base clas and find out where the event is raised.

如果基类没有基类,唯一的方法是声明一个新事件,并从引发新事件的基类向原始事件添加一个处理程序。请注意,如果您有其他代码使用基类'事件,并且evnet不是虚拟的并且没有提升方法,那么您运气不好,除非您可以反编译基类并找出事件的位置被提出来了。

#2


Events in WinForms typically have a corresponding "On*" method. Simply call that method, and it'll raise the event. If you want to raise "CellClick" for example, call "base.OnCellClick(new DataGridViewCellEventArgs(row, column))".

WinForms中的事件通常具有相应的“On *”方法。只需调用该方法,它就会引发事件。例如,如果要引发“CellClick”,请调用“base.OnCellClick(new DataGridViewCellEventArgs(row,column))”。

相关文章