事件参数分配

发布于 2024-09-04 21:26:02 字数 182 浏览 5 评论 0原文

我有这个事件处理程序

Temp.MouseLeftButtonDown += new MouseButtonEventHandler(Temp_MouseLeftButtonDown);

但我想发送一些参数以在 Temp_MouseLeftButtonDown 函数中访问。 我该如何分配它?

i have this event handler

Temp.MouseLeftButtonDown += new MouseButtonEventHandler(Temp_MouseLeftButtonDown);

but i wanna send some parameter to access in the Temp_MouseLeftButtonDown function.
how can i assign it ??

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

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

发布评论

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

评论(1

南笙 2024-09-11 21:26:02

您不能直接执行此操作,因为事件处理程序只能期望与 MouseButtonEventHandler 兼容的签名。

如果您使用 C# 3,最简单的方法是使用 lambda 表达式 - 类似于:

Temp.MouseLeftButtonDown +=
   (sender, args) => Temp_MouseLeftButtonDown(sender, args, "extra argument");

这有帮助吗?当然,如果您不需要发送者参数和事件参数,则不必提供它们。

在 C# 2 中,您可以以相同的方式使用匿名方法。

You can't do it directly, because the event handler can only expect a compatible signature with MouseButtonEventHandler.

If you're using C# 3, the easiest approach would be to use a lambda expression - something like:

Temp.MouseLeftButtonDown +=
   (sender, args) => Temp_MouseLeftButtonDown(sender, args, "extra argument");

Does that help? Of course, if you don't need both the sender and event args, you don't have to supply them.

In C# 2 you could use an anonymous method in the same way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文