在 C# 中手动触发事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1376878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
manually firing the event in c#
提问by Anuya
i want to fire an event manually using c#. For instance, say if i want to fire a Form_closing event of Form A from Form B. How to do it ??
我想使用 c# 手动触发一个事件。例如,假设我想从表单 B 触发表单 A 的 Form_closure 事件。怎么做??
After getting some comments. I think i need to explain more on this.
得到一些评论后。我想我需要对此进行更多解释。
Since my Form A is reference to a .dll which creates a custom taskbar in the desktop, There is a situation for me to close the that custom taskbar from Form B. i already tried FormA.Close() from Form B. when i do this, the .dll is unloaded from the app domain and due to this the space occupied by the custom task bar is blocked.
由于我的 Form A 引用了在桌面上创建自定义任务栏的 .dll,因此我有一种情况需要从 Form B 关闭该自定义任务栏。我已经尝试了 FormB 中的 FormA.Close()。当我这样做时这样,.dll 从应用程序域中卸载,因此自定义任务栏占用的空间被阻止。
But that is not the case when i click the close button in the custom task bar. when i do this the space is freed up.
但是当我单击自定义任务栏中的关闭按钮时,情况并非如此。当我这样做时,空间被释放了。
This is the reason i want to fire the close event of Form A manually from Form B which will solve my issue.
这就是我想从表单 B 手动触发表单 A 的关闭事件的原因,这将解决我的问题。
Thanks.
谢谢。
采纳答案by Evgeny
We did the following in one project:
我们在一个项目中做了以下事情:
There was a GlobalNotifier class, which defined events we wanted to use in different modules of the application, like this
有一个 GlobalNotifier 类,它定义了我们想在应用程序的不同模块中使用的事件,像这样
public static class GlobalNotifier
{
public static event VoidEventHandler EnvironmentChanged;
public static void OnEnvironmentChanged()
{
if (EnvironmentChanged != null)
{
EnvironmentChanged();
}
}
}
Then, you could raise this event anywhere when you needed to let the rest of the application know that the environment has changed, like this
然后,当您需要让应用程序的其余部分知道环境已更改时,您可以在任何地方引发此事件,如下所示
GlobalNotifier.OnEnvironmentChanged();
And also you could subscribe to this event wherever you wanted to be notified about the fact that the environment has changed.
此外,您还可以在任何您希望收到环境变化通知的地方订阅此事件。
public ReportingService()
{
GlobalNotifier.EnvironmentChanged += new VoidEventHandler(GlobalNotifier_EnvironmentChanged);
}
void GlobalNotifier_EnvironmentChanged()
{
//reset settings
_reportSettings = null;
}
So, whenever you changed the environment, you raised the event, and everyone who needed to know about that and perform some actions, was notified. Might be similar to what you need to achieve.
因此,每当您更改环境时,您都会引发该事件,并且每个需要了解该情况并执行某些操作的人都会收到通知。可能类似于您需要实现的目标。
Also, if you need to pass parameters, you can define the event any way you like, basically -
此外,如果您需要传递参数,您可以以任何您喜欢的方式定义事件,基本上 -
public static event VoidEventHandler<SomeObject, List<OtherObject>> SomethingUpdated;
public static void OnSomethingUpdated(SomeObject sender, List<OtherObject> associations)
{
if (SomethingUpdated != null)
{
SomethingUpdated(sender, associations);
}
}
// ...
MyClass.SomethingUpdated+= new VoidEventHandler<SomeObject, List<OtherObject>>(MyClass_SomethingUpdated);
// ...
void MyClass_SomethingUpdated(SomeObject param1, List<OtherObject> param2)
{
//do something
}
回答by Anuya
You would call the OnFormClosing()
method of the Form class. You can do this with any event that has a paired On...()
method.
您将调用OnFormClosing()
Form 类的方法。您可以对任何具有配对On...()
方法的事件执行此操作。
回答by Joel Coehoorn
Close the form. That will raise the event. If you want to raise the event separately from closing the form, you're doing something wrong; move that code to a separate method that you can call from the event and from FormB.
关闭表格。这将引发事件。如果您想与关闭表单分开引发事件,那么您就做错了;将该代码移动到一个单独的方法中,您可以从事件和 FormB 中调用该方法。
FormAInstance.Close()
Starting with Visual Studio 2005 (.Net 2.0), forms have automatic default instances. It sounds like that's what you're using. You should know that this feature exists mainly for backwards compatibility with VB6. You're really better off creating and using a new instance of the form. When you do that, you should just be able to call the .Close()
method on that instance without it's app domain closing.
从 Visual Studio 2005 (.Net 2.0) 开始,表单具有自动默认实例。听起来这就是你正在使用的。您应该知道此功能的存在主要是为了与 VB6 向后兼容。您最好创建和使用表单的新实例。当您这样做时,您应该能够.Close()
在该实例上调用该方法,而无需关闭应用程序域。
If you want to re-use this space, you might also try simply .Hide()
-ing the form rather than closing it.
如果你想重新使用这个空间,你也可以尝试简单地.Hide()
-ing 表单而不是关闭它。
回答by Ryan Lundy
It sounds to me, from your comments, like you don't want to raiseone form's event from the other; you just want to handleone form's event from the other.
在我看来,从您的评论来看,您不想从另一个表单中引发一个事件;您只想从另一个表单处理一个表单的事件。
Yes, you can do that. FormB
needs to have a reference to FormA
. There are several ways you can do this; one easy way is to have a FormA
type property in your FormB class, like this:
是的,你可以这样做。 FormB
需要参考FormA
。有几种方法可以做到这一点;一种简单的方法是FormA
在 FormB 类中有一个type 属性,如下所示:
public FormA TheOtherForm { get; set; }
You set that property to your instance of FormA
, and then you add the event handler as you've described in the comments.
您将该属性设置为 的实例FormA
,然后按照注释中的描述添加事件处理程序。
You don't have to use FormA
as the type of your property; any form has the FormClosing
event, so you can just use Form as the type if you want.
您不必使用FormA
作为您财产的类型;任何表单都有FormClosing
事件,因此您可以根据需要使用 Form 作为类型。