简体   繁体   English

Winform自定义控件如何通知另一个Winform自定义控件?

[英]How a Winform custom control can notify another Winform Custom Control?

Let's say in a custom control I defined some custom event myEvent. 假设在自定义控件中,我定义了一些自定义事件myEvent。 Now when raising event, this event will be captured by the parent form. 现在,当引发事件时,该事件将被父表单捕获。

How can I capture this event in another custom control which would be on the same parent form ? 如何在同一父窗体上的另一个自定义控件中捕获此事件? I'd like the other control to subscribe to the first control event somehow. 我希望其他控件以某种方式订阅第一个控件事件。

I've run into a similar situation a lot when dealing with MVC. 在处理MVC时,我遇到了很多类似的情况。 The way that I like to handle it is to use a mediator design pattern in the controller. 我喜欢处理的方式是在控制器中使用中介器设计模式。

Basically, you have a class that has a register function and a notify function. 基本上,您有一个具有注册功能和通知功能的类。 The register function takes an object that implements a listener interface and a messageId. register函数采用一个实现侦听器接口和messageId的对象。 It stores these in a dictionary. 它将它们存储在字典中。 The notify function takes a messageId for the event that needs to be sent to the listeners and notifies the appropriate ones that the event has occurred. notify函数获取需要发送给侦听器的事件的messageId,并通知适当的事件已发生。

So maybe something along the lines of 所以也许是这样的

public interface IListener
{
void MessageRaised(int messageId, params object[] arguments);
}

public class Mediator
{
public void Register(IListener listener, int messageId)
{
//... add to dictionary of arrays for each messageId
}

public void NotifyListeners(int messageId, params object[] arguments)
{
//... loop through listeners for the messageId and call the MessageRaised function for each one
}
}

Now normally I have a base controller and it implements a static Mediator object. 现在,通常我有一个基本控制器,它实现了一个静态介体对象。 Then all my other controllers inherit from it. 然后,我所有其他控制器都继承自它。 If you are using the code behind and cannot inherit, then you might try using a singleton pattern. 如果您正在使用后面的代码且无法继承,则可以尝试使用单例模式。 .Net static classes are pretty great too since they have a constructor so you could use that as well. .Net静态类也很棒,因为它们有一个构造函数,因此您也可以使用它。

So in your case, I would have the code behind for each control implement IListener and then in the constructor for each one, have something like Mediator.GetInstance().Register(this, Messages.MyEvent). 因此,在您的情况下,我将在每个控件实现IListener的后面都有代码,然后在每个控件的构造函数中都有诸如Mediator.GetInstance()。Register(this,Messages.MyEvent)之类的代码。 That is kind of a quick and dirty way which can be refactored a bit in the future to make it a bit more reusable. 这是一种快速而肮脏的方法,将来可以对其进行一些重构,以使其更具可重用性。

Some resources from a quick google search 谷歌快速搜索的一些资源

http://www.avajava.com/tutorials/lessons/mediator-pattern.html http://www.avajava.com/tutorials/lessons/mediator-pattern.html

http://sourcemaking.com/design_patterns/mediator http://sourcemaking.com/design_patterns/mediator

Good luck 祝好运

您可以使用事件处理和代表为和你的接收器控制间接订阅您发送器控制的事件,同时通过控件的父控件,充当多路复用器。

The answer is simple. 答案很简单。 Declare a method in the second form (the one without the event) with the correct signature and mark it as public. 以第二种形式(没有事件的形式)声明一种具有正确签名的方法,并将其标记为public。

In the parent form assign the method of the second form to the event of the first. 在父窗体中,将第二个窗体的方法分配给第一个窗体的事件。

是的,只需发布​​并引发一个事件并从您的侦听器控件中订阅它即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM