【问题标题】:C# - EventHandler is always nullC# - EventHandler 始终为空
【发布时间】:2016-02-22 17:49:48
【问题描述】:

我正在尝试在 UserControl 和 Form 之间实现一个非常基本的 EventHandler。关于订阅活动,我做错了什么?无论我尝试什么, CreateButtonEvent 始终为空。我最初试图在两个 UserControl 类之间执行此操作,但在假设 UserControl 订阅可能是问题的情况下决定使用 Form 作为订阅者。我也尝试过使用委托,但没有成功。

我已经在这个站点上查看并实施了无数解决方案来解决这个完全相同的问题,但我仍然无法让 Form 类订阅 UserControl 类中的事件。我确信这是一个非常简单的错误,我无法挑出。谁能给我一些见解?

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

这是表单类

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();
        }
    }
}

【问题讨论】:

  • 从哪里调用 mehog goToCreate 在您的用户控制中?
  • 不,根据您显示的代码,事件已正确连接。奇怪的是,gotoCreate 上的 LoginUserControl 本身看起来就像一个 EventHandler。您必须显示更多代码才能获得帮助(例如如何在表单上显示 LoginUserControl)。
  • @Jonesopolis 你的意思是用户控件在表单中的可视化表示吗?
  • 您确定订阅了正确的 LoginUserControl 吗?我想知道您的表单中是否还有另一个 LoginUserControl,可能在 ApplicationContainer.Designer.cs 中定义?

标签: c# winforms


【解决方案1】:

你的问题在这里:

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

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

您正在创建一个LoginUserControl 作为表单中的变量并订阅它,但您还没有将它添加到表单的任何地方。如在,没有你有Children.Add(login) 的地方。

我猜您在设计器中放置了另一个LoginUserControl 副本,这就是您在运行应用程序时与之交互的那个副本。该事件始终为空,因为您已在其他用户控件上订阅了该事件。

转到设计器,单击用户控件,转到属性 (F4),单击事件按钮,找到 CreateButtonEvent 并添加您的 gotoCreate 方法。

然后删除您创建的成员变量login,因为这只会造成混淆。

同样,CreateRecUserControl 也一样。如果没有在设计器中添加,请将其添加到您的设计器中并删除您的成员变量createRec

【讨论】:

    【解决方案2】:

    我准确地测试了您的代码,一切正常。我认为这可能是其中一个问题:

    • 1:此代码永远不会运行:

           public ApplicationContainer()
       {
           InitializeComponent();
           login.CreateButtonEvent += gotoCreate;
       }
      
    • 或 2:您在代码中的某个位置触发事件,如下所示:

      login.CreateButtonEvent -= gotoCreate;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 2020-08-24
      相关资源
      最近更新 更多