c# - 如何引发 MouseClick 事件?

标签 c# events

你好 我读过可以像方法一样引发事件。嗯,它适用于我的自定义事件(我创建了一个委托(delegate),事件,我可以通过调用它来引发事件)。 但是,我无法手动引发 MouseClick 等事件,它一直说它必须出现在 += 运算符的左侧。有什么问题?

最佳答案

虽然我确信您会得到比这个答案更丰富的其他答案,但基本上您不能在包含它的类之外“引发”事件。 MSDN 有 this to say关于事件

Events are a special kind of multicast delegate that can only be invoked from within the class or struct where they are declared (the publisher class). If other classes or structs subscribe to the event, their event handler methods will be called when the publisher class raises the event.

如果您想真正引发事件,例如,Windows 窗体控件鼠标点击,您必须创建该控件的子类并调用 base.OnMouseClick() 或覆盖它。

关于c# - 如何引发 MouseClick 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5410590/

相关文章:

C# 如何检测互联网连接/断开(带有事件)?

javascript - 如何防止其他点击事件在其中一个已在 jQuery 中运行时运行?

c# - 带有 EF6 的 WCF - 在应用程序配置文件中找不到名为 'Entities' 的连接字符串,[我设置]

c# - 如何向 Html.ActionLink() url 添加描述性文本

c# - 控制只浏览/选择文件夹,而不是 ASP.NET 中的文件?

iphone - UIControlEventTouchDragExit 仅触发 ~100 像素

c# - 在 C# 中使用 += 运算符将方法绑定(bind)到事件

javascript - 单击时会触发拖动事件

c# - Azure 管道日志在运行任务中添加 C# 消息

c# - 加速 LINQ 插入