WinForms:如何以编程方式触发事件处理程序?

编程入门 行业动态 更新时间:2024-10-23 03:18:30
本文介绍了WinForms:如何以编程方式触发事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想以编程方式调用控件的事件处理程序。例如:

I want to programmatically invoke an event handler for a control. For example:

DateTimePicker dtpLastConsummated;

我想触发 dtpLastConsummated ,我该怎么做?

I want to trigger the TextChanged event handler for the dtpLastConsummated, how can i do it?

在其他语言中,我会称呼类似于:

In other languages I would call something akin to:

dtpLastConsummated.TextChanged(this, new EventArgs());

但是在.NET中,您可以有多个事件处理程序:

but in .NET you can have multiple event handlers:

dtpLastConsummated.Click +=new EventHandler(dtpLastConsummated_TextChanged); dtpLastConsummated.Click +=new EventHandler(dtpLastConsummated_AnotherHandler); dtpLastConsummated.Click +=new EventHandler(dtpLastConsummated_MoreHandlers); ... dtpLastConsummated.Click +=new EventHandler(dtpLastConsummated_Nminus1);

所以您需要一种触发所有附加事件处理程序的方法。

so you need a way to trigger all the attached event handlers.

以下代码将触发该事件:

The following code will fire the event:

Toolkit.FireEvent(dtpLastConsummated, "TextChanged", new EventArgs());

这是静态工具包功能的代码:

And here's the code of the static toolkit function:

/// <summary> /// Programatically fire an event handler of an object /// </summary> /// <param name="targetObject"></param> /// <param name="eventName"></param> /// <param name="e"></param> public static void FireEvent(Object targetObject, string eventName, EventArgs e) { /* * By convention event handlers are internally called by a protected * method called OnEventName * e.g. * public event TextChanged * is triggered by * protected void OnTextChanged * * If the object didn't create an OnXxxx protected method, * then you're screwed. But your alternative was over override * the method and call it - so you'd be screwed the other way too. */ //Event thrower method name //e.g. OnTextChanged String methodName = "On" + eventName; MethodInfo mi = targetObject.GetType().GetMethod( methodName, BindingFlags.Instance | BindingFlags.NonPublic); if (mi == null) throw new ArgumentException("Cannot find event thrower named "+methodName); mi.Invoke(targetObject, new object[] { e }); }

注意: 我不会在.NET框架中创建每个控件的子类,也不在每个第三方控件中创建一个子类,而是说服一个企业级的开发人员改造每个表单以使用我的自定义控件。

Note: I'm not creating a subclass of every control in the .NET framework, and every 3rd party control, and convincing an enterprise worth of developers to retrofit every form to use my custom controls.

推荐答案

3条引发TextChanged事件的建议:

3 suggestions to fire the TextChanged Event:

手动更改文本:

string s = dateTimePicker1.Text; dateTimePicker1.Text = String.Empty; dateTimePicker1.Text = s;

从DateTimePicker继承并创建一个公开/调用DateTimePicker受保护的OnTextChanged的新方法

Inherit from DateTimePicker and create a new method that exposes / calls DateTimePicker's protected OnTextChanged

public class MyDateTimePicker : DateTimePicker { public void OnTextChanged(EventArgs e) { base.OnTextChanged(e); } }

如果您不喜欢OOP并想破坏封装,则可以通过反射访问受保护的OnTextChanged方法:

If you don't like OOP and want to break encapsulation, you can access the protected OnTextChanged method through reflection:

MethodInfo onTextChanged = dateTimePicker1.GetType().GetMethod("OnTextChanged", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); onTextChanged.Invoke(dateTimePicker1, new object[] { new EventArgs() });

更多推荐

WinForms:如何以编程方式触发事件处理程序?

本文发布于:2023-11-26 00:04:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631871.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   方式   程序   WinForms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!