简体   繁体   English

C# - EventHandler始终为null

[英]C# - EventHandler is always null

I am trying to implement a very basic EventHandler between a UserControl and a Form. 我试图在UserControl和Form之间实现一个非常基本的EventHandler。 What am I doing wrong with regards to subscribing to the event? 订阅活动我做错了什么? No matter what I try, the CreateButtonEvent is always null. 无论我尝试什么,CreateButtonEvent始终为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. 我原本试图在两个UserControl类之间执行此操作,但在假设UserControl订阅可能是问题的情况下决定使用Form作为订阅者。 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. 我已经在这个网站上查看并实现了无数的解决方案,我仍然无法让Form类订阅UserControl类中的Event。 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 这是UserControl类

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 这是Form类

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. 您正在创建一个LoginUserControl作为表单中的变量并订阅它,但您尚未在任何地方将其添加到表单中。 As in, there is no place where you have Children.Add(login) . 在,你没有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. 我猜你在表单上有另一个LoginUserControl副本放在设计器中,那就是你在运行应用程序时要与之交互的副本。 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. 转到设计器,单击用户控件,转到属性(F4),单击事件按钮,找到CreateButtonEvent并添加gotoCreate方法。

Then remove the member variable login that you've created, because that will just be confusing. 然后删除您创建的成员变量login ,因为这只会令人困惑。

Also, same with CreateRecUserControl . 此外,与CreateRecUserControl相同。 If it's not added in the designer, add it in your designer and remove your member variable createRec . 如果它未在设计器中添加,请将其添加到设计器中并删除成员变量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: 1:此代码永远不会运行:

      public ApplicationContainer() { InitializeComponent(); login.CreateButtonEvent += gotoCreate; } 
  • or 2: you off it some where in your code like this: 或者2:你把代码放在代码中的某个地方:

    login.CreateButtonEvent -= gotoCreate; login.CreateButtonEvent - = gotoCreate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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