WinForms/.NET:如何获取附加到事件的事件处理程序?

发布于 2024-12-16 18:45:41 字数 411 浏览 1 评论 0原文

如何将事件处理程序附加到事件?

伪代码,例如:

public static EventHandler GetChangeUICuesEventHandler(Control SomeControl)
{
   EventHandler changeUICuesEventHandler = SomeControl.ChangeUICues;
   return changeUICuesEventHandler
}

示例用法(伪代码):

Control control = GetControlByName(Console.ReadLine());
button1.ChangeUICues += GetChangeUICuesEventHandler(control);

How can i get the event handler attached to an event?

pseudocode, for example's sake:

public static EventHandler GetChangeUICuesEventHandler(Control SomeControl)
{
   EventHandler changeUICuesEventHandler = SomeControl.ChangeUICues;
   return changeUICuesEventHandler
}

Sample usage (pseudo-code):

Control control = GetControlByName(Console.ReadLine());
button1.ChangeUICues += GetChangeUICuesEventHandler(control);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月蹉跎了容颜 2024-12-23 18:45:41

我提供以下内容作为替代路径而不是直接答案...所以这里...

您可以通过确保您的控件已附加事件处理程序(在本例中为参数someControl,而是实现一个定义附加处理程序的接口。例如:

public interface ChangUICuesHandler
{
    void OnChangeUICues(object sender, EventArgs e);
}

那么你可以像这样使用它:

Control control = GetControlByName(Console.ReadLine());
if (!(control is ChangeUICuesHandler))
{
    throw new Exception("Input Control does not implement the interface");
}
var handlerControl = control as ChangeUICuesHandler;
button.ChangeUICues += handler.OnChangeUICues;

I offer up the following as an alternative path rather than a direct answer... So here goes...

Can you approach this a little differently perhaps by ensuring that your control with the event handler already attached, in this case the parameter someControl, instead implements an interface that defines the attached handler. For example:

public interface ChangUICuesHandler
{
    void OnChangeUICues(object sender, EventArgs e);
}

Then you could use it like so:

Control control = GetControlByName(Console.ReadLine());
if (!(control is ChangeUICuesHandler))
{
    throw new Exception("Input Control does not implement the interface");
}
var handlerControl = control as ChangeUICuesHandler;
button.ChangeUICues += handler.OnChangeUICues;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文