title: Windows Forms Event Subscription c#
body: <p>I have a sample form with 3 windows. Each window has a label and the main form has a button.</p>
  
  <p>I have a class with the following code:</p>
  
  <pre><code>    public class CustomEventArgs : EventArgs
  {
      public string Message { get; set; }
      public CustomEventArgs(string message)
      {
          Message = message;
      }
  }
  
  public delegate void CustomEventHandler(object sender, CustomEventArgs args);
  
  public class EventCode
  {
      public void Process()
      {
          var cea = new CustomEventArgs("I was processed");
          if (MyEvent != null)
          {
              MyEvent.Invoke(this, cea);
          }
      }
  
      public event CustomEventHandler MyEvent;
  }
  </code></pre>
  
  <p>On my main form, I am going to push the button and have it process code in my EventCode class, and then invoke the event. I want the event to send a message to both forms that are open, which will then display a message on the screen.</p>
  
  <p>Here is my button click code in Form1:</p>
  
  <pre><code>        private void Form1_Load(object sender, EventArgs e)
      {
          Window1 w1 = new Window1();
          w1.Show();
  
          Window2 w2 = new Window2();
          w2.Show();
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          EventCode ec = new EventCode();
          ec.Process();
      }
  </code></pre>
  
  <p>In Window 1: </p>
  
  <pre><code>EventCode ec = new EventCode();
      public Window1()
      {
          InitializeComponent();
          ec.MyEvent += new CustomEventHandler(ec_MyEvent);
      }
  
      void ec_MyEvent(object sender, CustomEventArgs args)
      {
          label1.Text = args.Message;
      }
  </code></pre>
  
  <p>Repeated for Window 2</p>
  
  <p>When I click on the button, the event subscription does not display the text on each of the forms. If i create an event subscription on the main form, it will display. </p>
  
  <p>I am not sure which route to take to make this functionality to work.</p>
  
  <p>Thoughts?</p>