-1

I'm trying to fire an event when button is clicked. I click the button and nothing happens.

The problem is I always get null as EventA in OnEventA():

namespace eventsC
{
    public partial class Form1 : Form
    {
        public event EventHandler EventA;

        protected void OnEventA()
        {
            if (EventA != null)
                //never arrives here as EventA is always = null
                EventA(this, EventArgs.Empty);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OnEventA();
        }
    }
}

EDIT:

Based on link from Henk Holterman I tested this code also:

public delegate void Eventhandler(object sender, Eventargs args);  

// your publishing class
class Foo  
{
    public event EventHandler Changed;    // the Event

    protected virtual void OnChanged()    // the Trigger method, called to raise the event
    {
        // make a copy to be more thread-safe
        EventHandler handler = Changed;   

        if (handler != null)
        {
            // invoke the subscribed event-handler(s)
            handler(this, EventArgs.Empty);  
        }
    }

    // an example of raising the event
    void SomeMethod()
    {
       if (...)        // on some condition
         OnChanged();  // raise the event
    }
}

I call OnChanged() when I click a button but the result is still always the same: EventA = null.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
PeterPam
  • 337
  • 6
  • 20

2 Answers2

0

I solved my problem in this way:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace eventsC
{    

    public partial class Form1 : Form
    {

        public static event EventHandler<EventArgs> myEvent;

        protected void OnMyEvent()
        {
            if (myEvent != null)
                myEvent(this, EventArgs.Empty);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myEvent += Handler;

            //call all methods that have been added to the event
            myEvent(this, EventArgs.Empty);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OnMyEvent();
        }


        static void Handler(object sender, EventArgs args)
        {
            Console.WriteLine("Event Handled!");
        }

    }
}
PeterPam
  • 337
  • 6
  • 20
  • 2
    This doesn't appear to answer your question. Your question seems to ask about consuming your event from a different class. Is that right? – Enigmativity May 01 '19 at 08:04
  • It doesn't really make sense to override `OnMyEvent` and also handle the event in your own class. The event handler should be done in the subscribing class, not in `Form1` – Chris Dunaway May 01 '19 at 14:48
0

I think they have correctly answered that it does not have any subscriber.

namespace TestConsoleApp
{
    public delegate void Eventhandler(object sender, EventArgs args);

    // your publishing class
    class Foo
    {
        public event EventHandler Changed;    // the Event

        protected virtual void OnChanged()    // the Trigger method, called to raise the event
        {
            // make a copy to be more thread-safe
            EventHandler handler = Changed;

            if (handler != null)
            {
                // invoke the subscribed event-handler(s)
                handler(this, EventArgs.Empty);
            }
        }

        // an example of raising the event
        public void SomeMethod()
        {

                OnChanged();  // raise the event
        }
    }

    public class Program
    {

        static void Main(string[] args)
        {
            Foo objFoo = new Foo();
            objFoo.Changed += ObjFoo_Changed;
            objFoo.SomeMethod();
            Console.ReadLine();
        }

        private static void ObjFoo_Changed(object sender, EventArgs e)
        {
            Console.WriteLine("Event fired and changed");
        }
    }
  }
Enigmativity
  • 113,464
  • 11
  • 89
  • 172