Manually activating an event in C #

I want to trigger an event manually using C #. For example, let's say if I want to fire the Form_closing event of form A from form B. How to do this?

After receiving comments. I think I need to explain more about this.

Since my form A is a link to a .dll that creates a custom taskbar on the desktop, for me there is a situation where I close this taskbar from form B. I already tried FormA.Close () from form B. when I I do it. The DLL is unloaded from the application domain, and because of this, the space occupied by the custom taskbar is blocked.

But this is not the case when I click the close button on the taskbar. when I do this, space is freed.

It is for this reason that I want to trigger the closing event of form A manually from form B, which will solve my problem.

Thanks.

+4
source share
4 answers

In one project, we did the following:

There was a GlobalNotifier class that defined events that we wanted to use in different application modules, for example

public static class GlobalNotifier { public static event VoidEventHandler EnvrionmentChanged; public static void OnEnvrionmentChanged() { if (EnvrionmentChanged != null) { EnvrionmentChanged(); } } } 

You can then raise this event anywhere you want the rest of the application to know that the environment has changed, for example

  GlobalNotifier.OnEnvrionmentChanged(); 

You can also subscribe to this event, wherever you want to be notified that the environment has changed.

  public ReportingService() { GlobalNotifier.EnvrionmentChanged += new VoidEventHandler(GlobalNotifier_EnvrionmentChanged); } void GlobalNotifier_EnvrionmentChanged() { //reset settings _reportSettings = null; } 

So, whenever you changed the environment, you raised this event, and everyone who should have known about this and performed some actions was notified. You may be similar to what you need to achieve.

Also, if you need to pass parameters, you can define the event in any way, 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 } 
+13
source

You would call the OnFormClosing() method of the Form class. You can do this with any event that has a paired On...() method.

+1
source

Close the form. This will raise this event. If you want to raise an event separately from closing the form, you are doing something wrong; move this code to a separate method that you can call from the event and from FormB.

 FormAInstance.Close() 

Starting with Visual Studio 2005 (.Net 2.0), forms have automatic instances by default. It looks like you are using. You should know that this function exists mainly for backward compatibility with VB6. You really are better off creating and using a new instance of the form. When you do this, you should simply call the .Close() method on this instance without closing the application domain.

If you want to reuse this space, try simply .Hide() -representing the form, rather than closing it.

+1
source

It seems to me from your comments, as if you do not want to raise one form event from another; you just want to handle one form event from another.

Yes you can do it. FormB must be a reference to FormA . There are several ways to do this; one simple way is a property of type FormA in your FormB class, for example:

 public FormA TheOtherForm { get; set; } 

You set this property to your FormA instance, and then add an event handler, as you described in the comments.

You do not need to use FormA as the type of your property; any form has a FormClosing event, so you can just use the form as a type if you want.

+1
source

All Articles