c# event handling between two forms

.net, c#, events

Solution

In your constructor, after instantiating `frmEventGenerate`:

frmGen.Evt += ReceiveEvent;

You don't need `new LinkEventHandler(...)` any more - as of C# 2, there's a method group conversion available which you can use to convert from a method group (the name of a method) to a delegate type.

EDIT: I hadn't seen that your event was static. That suggests you should actually use:

frmEventGenerate.Evt += ReceiveEvent;

... and you don't need the `frmGen` variable at all.

However, I would strongly discourage you from this - why do you want the event to be static in the first place? (I'd also urge you to name your types more sensibly - something like "EventGenerator" would be better here, for example. Ignoring the convention that type names should be in Pascal case leads to confusing code.)

Problem

I have two forms and I am trying to capture an event generated from frmEventGenerate.cs in frmEventReceive.cs. In this example I can receive the event from frmEventGenerate.cs but not sure how I can catch this in frmEventReceive.cs? frmEventReceive.cs is my startup form which creates frmEventGenerate.cs. Can someone point me in the right direction, I think I am being stupid! Thank you frmEventGenerate.cs: ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Events { public delegate void LinkToEventHandler(); public partial class frmEventGenerate : Form { public static event LinkToEventHandler Evt; public frmEventGenerate() { InitializeComponent(); Evt += new LinkToEventHandler(ReceiveEvent); SendEvent(); } public static void SendEvent() { if (Evt != null) { Evt(); } } public void ReceiveEvent() { System.Console.WriteLine("Received Event - This works ok"); } } } ``` frmEventReceive.cs: ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Events { public partial class frmEventReceive : Form { public frmEventReceive() { InitializeComponent(); frmEventGenerate frmGen = new frmEventGenerate(); } public void ReceiveEvent() { System.Console.WriteLine("I want to be able to receive the even here!"); } } } ```

Original source