如何在C#winforms中的其他类中监听类中的事件处理程序? [英] How to listen a event handler in a class from other class in C# winforms?
本文介绍了如何在C#winforms中的其他类中监听类中的事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个类,我在一个类中初始化事件处理程序。我需要从另一个类听这个事件处理程序。我可以通过哪种方式实现这个目标?
我的尝试:
我已经尝试了,
公共事件EventHandler NetworkLostEvent;
i使用此代码解雇event NetworkLostEvent(this,EventArgs.Empty);
如何从其他类中听这个事件?
解决方案
< blockquote>你所要做的就是为类实例事件添加一个处理程序。
看看这个:在两种形式之间转移信息,第2部分:儿童到父母 [ ^ ]它演示了从两个类创建,触发和处理事件(在这种情况下是表单,但是所有类以相同的方式工作)
I have two classes, I Initialize Event handler in one class. and i need to listen this event handler from another class. In which way i can implement this ?
What I have tried:
I have already try,
public event EventHandler NetworkLostEvent;
i use this code to fire the event NetworkLostEvent(this, EventArgs.Empty);
How to listen this event from other class?
解决方案
All you have to do is add a handler to the class instance event.
Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] it demonstrates creating, firing, and handling an event from two classes (in this case forms but all classes work in identical ways)
这篇关于如何在C#winforms中的其他类中监听类中的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文