如何从另一个处理程序调用/调用按钮单击事件处理程序? (c#)
我一定错过了一些明显的东西 - 我是 .NET 的新手 - 多年来一直在 C++/MFC 中开发。
在按钮单击的事件处理程序中,我想重新使用另一个事件处理程序/单击。但是,我似乎不知道该怎么做。
可能引起麻烦的一点是我想要“触发”的第二个项目的处理程序不在同一表单/上下文中。
看来我应该只能调用该方法......
但我似乎无法做到这一点。
这是在紧凑框架/Win Mobile 中
I must be missing something obvious - I am new to .NET - been developing in C++/MFC for years.
In an event handler for a button click I would like to then re-use another event handler/click. However, I can't seem to figure out how to do that.
The bit that may be causing trouble is that the handler for the second item I want to "fire" is not on the same form/context.
It seems that I should just be able to call the method...
But I don't seem to be able to do it.
This is in compact framework/Win Mobile
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的无知。我只是看不到其他处理程序。这是一个要解决的简单问题 - 我只是使处理程序对处理第二个事件的对象/表单可见,并且很容易触发另一个事件。
My ignorance. I just didn't have visibility to the other handler. This is a simple problem to solve - I just made the handler visible to the object/form that handles the second event and it is easy to fire the other event.
您需要执行以下操作之一:
You need to do one of the following:
将公共代码分解为私有方法怎么样?然后您可以从两个处理程序调用该方法。
How about you factor out the common code into a private method? Then you can call that method from both handlers.
我认为更好的解决方案是重构代码,以便原始事件处理程序完成的工作包含在函数中。
然后原始处理程序和新处理程序都调用相同的函数。
I think a better solution would be to refactor your code so that the work done by the original event handler is contained within a function.
Then both the original handler and the new handler both call the same function.
在事件处理程序 1 内部,只需引发事件处理程序 2 正在“侦听”的事件。因此,在事件处理程序 1 中放置
OnEvent()
。请注意,事件处理程序 2 必须已连接才能侦听该事件。如果您显示更多代码,我可以更具体地说,但这可能就足够了。
Inside of event handler 1, just raise the event that event handler 2 is "listening" for. So inside event handler 1, put
OnEvent()
. Note that event handler 2 must have already been hooked up to listen to the event.I can speak in more specificity if you show some more code, but this may be enough.