4

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

    }
}
Belliez
  • 5,356
  • 12
  • 54
  • 62

2 Answers2

3

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Don't forget to unregister the event if you ever close frmEventReceive but not the frmEventGenerate – nos Feb 03 '10 at 14:32
  • 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 Feb 03 '10 at 14:49
  • @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 Feb 03 '10 at 14:51
  • 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 Feb 03 '10 at 14:53
0
//Receiver

using System;
using System.Windows.Forms;

namespace eTest
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        public void ReceiveEvent(int i)
        {
            MessageBox.Show("Event Received from Form: " + i.ToString());
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            int num = 0;
            int x = 0;

            num = Convert.ToInt32(txtForms.Text);

            for (x = 0; x < num; x++)
            {
                frmDL f = new frmDL();
                f.Evt += ReceiveEvent;
                f.iID = x;
                f.Text = x.ToString(); 
                f.Show();
                f.Activate();
                Application.DoEvents();
            }
        }
    }
}

//Sender

using System;
using System.Windows.Forms;

namespace eTest
{
    public delegate void myEventHandler(int i);

    public partial class frmDL : Form
    {
        public event myEventHandler Evt;

        public int iID = 0;

        public frmDL()
        {
            InitializeComponent();
        }

        public void SendEvent()
        {
            if (Evt != null)
            {
                Evt(this.iID);
            }
        }

        private void btnEvent_Click(object sender, EventArgs e)
        {
            SendEvent();
        }
    }
}
Naudy J
  • 1
  • 2
  • 1) Create C# WFA Project. 2) Create a form: 'frmMain' [receiver] Add a btn called 'btnNew' and a text field called 'txtNum'. This field is for the number of forms to create. Enter a default value of 3 for example. 3) Create a 2nd form: frmDL [sender] add a button called 'btnEvent'. This will fire the event with the form id set by frmMain. – Naudy J Sep 16 '11 at 01:29
  • 1
    Rather than only post a block of code, please *explain* why this code solves the problem posed. Without an explanation, this is not an answer. – Martijn Pieters Nov 11 '12 at 22:04