覆盖派生的事件 [英] Overriding a derived Event

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

问题描述

我的问题涉及以下主题:http://www.codeproject.com/Questions/992555/Event-outside-of-a-collection?arn=23



我想从派生类中覆盖一个事件 - 例如来自Forms-Control。



我的实际状态是,压倒一切(当我直接使用此控件的处理程序时,由命令阴影执行。

控件是集合的成员,它只与我自己创建的此类事件一起使用 - 如果我尝试使用重写的事件它不起作用。我认为Collection使用了Base-Class中的Event。

这可能吗?

如果是 - 我该怎么办?

My question refers to the following Thread : http://www.codeproject.com/Questions/992555/Event-outside-of-a-collection?arn=23

I want to "override" an Event from a derived class - for example from a Forms-Control.

My actual State is, that the overriding (performed by the Command "Shadows") is working when I use the Handler of this Control directly.
Is the Control a member of a Collection it is only working with such Events which I have created by myself - if I try to use the overridden Event it isn't working. I suppose that the Collection uses the Event from the Base-Class.
Is that possible ?
And if "Yes" - what could I do ?

推荐答案

在Sergey(-SA)的帮助下,在另一个完全不同主题的问题中,我自己找到了解决方案。

必须完成使用Reflection并在Parent-Control中调用Event-method。



这是一个代码片段,显示了如何完成:

With the help of Sergey (-SA), in another question with a complete different topic, I found the Solution by myself.
It must be done with Reflection and calling the Event-method in the Parent-Control.

Here is a code-snippet which shows how it could be done :
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

     Dim onclick1 As System.Reflection.MethodInfo = Button1.[GetType]().GetMethod("OnClick", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)
     onclick1.Invoke(Button1, New Object() {New EventArgs()})

 End Sub





在这种情况下,Button2使用该方法在Button1里面。

事件由Button1而不是Button2引发。



再次在这个地方感谢谢尔盖的帮助和灵感...... :)



In this case the Button2 uses the method inside the Button1.
The Event is raised by Button1 and not by Button2.

At this place again my thank to Sergey for his assistance and inspiration ... :)


这篇关于覆盖派生的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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