tags:

views:

207

answers:

1

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!");
        }

    }
}
+2  A: 

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.)

Jon Skeet
Don't forget to unregister the event if you ever close frmEventReceive but not the frmEventGenerate
nos
I removed the static declaration now, this was put in when I was experimenting. Added frmGen.Evt += ReceiveEvent; but still cannot receive the event!
Belliez
@Belliez: Now that it's a per-instance event, you'll need to make sure that the right instance is raising the event. You haven't shown what's actually raising the event at the moment.
Jon Skeet
I just realised this after I left the comment, the event was being fired before the form had loaded. Just re-coding now to test.
Belliez