简体   繁体   中英

C# - EventHandler is always null

I am trying to implement a very basic EventHandler between a UserControl and a Form. What am I doing wrong with regards to subscribing to the event? No matter what I try, the CreateButtonEvent is always null. I was originally trying to do this between two UserControl classes but decided to use a Form as the subscriber on the assumption that the UserControl subscription may have been the issue. I have also tried using delegates, with no success.

I have viewed and implemented countless solutions to this exact same questions on this site and I still cannot get the Form class to subscribe to the Event within the UserControl class. I am sure it is a very simple mistake that I just cannot pick out. Can anyone give me some insight?

Here is the UserControl class

using System;
using System.Windows.Forms;

namespace SequenceAutomation
{
    public partial class LoginUserControl : UserControl
    {
        public event EventHandler CreateButtonEvent;

        public LoginUserControl()
        {
            InitializeComponent();
        }

        protected void gotoCreate(object sender, EventArgs e)
        {
            if (CreateButtonEvent != null)
                CreateButtonEvent(this, e);
            else
                Console.WriteLine("CreateButtonEvent is null");
        }
    }
}

Here is the Form class

using System;
using System.Windows.Forms;

namespace SequenceAutomation
{
    public partial class ApplicationContainer : Form
    {
        private LoginUserControl login = new LoginUserControl();
        private CreateRecUserControl createRec = new CreateRecUserControl();

        public ApplicationContainer()
        {
            InitializeComponent();
            login.CreateButtonEvent += gotoCreate;
        }

        protected void gotoCreate(object sender, EventArgs e)
        {
            login.Hide();
            createRec.Show();
        }
    }
}

Your problem is here:

    private LoginUserControl login = new LoginUserControl();
    private CreateRecUserControl createRec = new CreateRecUserControl();

    public ApplicationContainer()
    {
        InitializeComponent();
        login.CreateButtonEvent += gotoCreate;
    }

You are creating a LoginUserControl as a variable in your form and subscribing to it, but you haven't added it to your form anywhere. As in, there is no place where you have Children.Add(login) .

I'm guessing you have another copy of the LoginUserControl on your form that you placed in the designer, and that's the one that you're interacting with when you run your application. The event is always empty because you've subscribed to that event on a different user control.

Go to the designer, click the user control, go to properties (F4), click the events button, locate the CreateButtonEvent and add your gotoCreate method.

Then remove the member variable login that you've created, because that will just be confusing.

Also, same with CreateRecUserControl . If it's not added in the designer, add it in your designer and remove your member variable createRec .

在此输入图像描述

I test your code exactly and it was OK. I think it could be one of this mistakes:

  • 1: this code is never run:

      public ApplicationContainer() { InitializeComponent(); login.CreateButtonEvent += gotoCreate; } 
  • or 2: you off it some where in your code like this:

    login.CreateButtonEvent -= gotoCreate;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM