“未设置对象引用..“向另一个窗体添加控件时出现异常

本文关键字:控件 异常 添加 窗体 设置 对象引用 另一个 | 更新日期: 2023-09-27 18:34:45

当用户

单击当前在面板中的用户控件上的按钮(UserControl1(时,我想将用户控件(UserControl2(添加到另一个窗体(MainForm(上的面板。 单击此按钮时,UserControl2应将UserControl1替换为面板的内容。

我已经尝试弄清楚如何使用事件在UserControl1MainForm之间进行通信,但我只是不知道从哪里开始,因为我找不到一个易于适应我特定情况的示例(将控件添加到面板(。

这个问题很相似,但不太适合我正在尝试做的事情(或者至少我只是不知道如何使其适应我正在尝试做的事情(

我尝试了这种方法,没有收到任何错误,但我的按钮没有任何作用:

主窗体:

    private void SubscribeToEvent(MyUserControl1 Button_Click)
    {
        MyUserControl1 CurrentClass = new MyUserControl1();
        CurrentClass.Button.Click += new EventHandler(this.ButtonClickHandler);
    }
    private void ButtonClickHandler(object sender, EventArgs e)
    {
        MyUserControl2 MainPanelControls = new MyUserControl2();
        MainPanel.SuspendLayout();
        MainPanel.Controls.Clear();
        MainPanel.Controls.Add(MainPanelControls);
        MainPanel.ResumeLayout();
    }

用户控件 1:

    public void Button_Click(object sender, EventArgs e)
    {
        //... Not sure what I'm missing here
    }

我也尝试过这种方法,这次尝试实现类似于我问题顶部附近链接中描述的方法。我知道这些显然是错误的(否则我不需要问这个问题(,但我对这个主题没有足够的知识来自己弄清楚:

用户控件 1:

    public MyUserControl1()
    {
        InitializeComponent();
    }
    private MainForm mainForm = null;
    public MyUserControl1(Form callingForm)
    {
        mainForm = callingForm as MainForm;
        InitializeComponent();
    }
//...
    private void Button_Click(object sender, EventArgs e)
    {
        MyUserControl2 MainPanelControls = new MyUserControl2();
        mainForm.MainPanel.SuspendLayout();
        mainForm.MainPanel.Controls.Clear();
        mainForm.MainPanel.Controls.Add(MainPanelControls);
        mainForm.MainPanel.ResumeLayout();
    }

现在,当我单击Button时,我在mainForm.MainPanel.SuspendLayout();(或超过该点的任何内容(出现"Object reference not set to an instance of an object"错误。

我也尝试修改 Joh 的答案,但最终得到相同的 Null 引用异常,这次在我的 Button_Click ButtonClickedToMainForm(this, e);事件中,我不确定我是否需要创建 ButtonClickedToMainForm 的实例,或者如何正确执行此操作(如果不是其他东西,那就是(。

有一种感觉,我可能只是把其中一些代码放在了错误的地方,所以我希望更有经验的人能够帮助我解决这个问题。

更新

这是我尝试实现Joh的答案,对此太陌生了,我不太确定我在哪里搞砸了:

主窗体:

namespace MyProject
{
public delegate void ButtonClickToMainForm(object sender, EventArgs e);
public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        UserControl1 userControl1 = new UserControl1();
        userControl1.Button1Clicked += userControl1_Button1Clicked;
    }
    private void userControl1_Button1Clicked(object sender, EventArgs e)
    {
        try
        {
            UserControl2 MainPanelControls = new UserControl2();
            MainPanel.SuspendLayout();
            MainPanel.Controls.Clear();
            MainPanel.Controls.Add(MainPanelControls);
            MainPanel.ResumeLayout();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
  }
}

用户控件 1:

public partial class UserControl1: UserControl
{
    public event ButtonClickToMainForm Button1Clicked;
    public UserControl1()
    {
        InitializeComponent();
    }
    private void OnButton1Clicked(object sender, EventArgs e)
    {
        Button1Clicked?.Invoke(this, e);
    }
    private void Button1_Click(object sender, EventArgs e)
    {
        Button1Clicked(this, e);  //"Object reference not set to an instance of an object."
    }
}

我确定这是我在UserControl1上缺少的简单东西,我只是不确定是什么。

“未设置对象引用..“向另一个窗体添加控件时出现异常

下面是委托事件的简单示例。当您在所有用户控件中实现它时,它应该适用于您的问题。

用户控件:

//Create a delegate
    public delegate void ButtonClickToMainForm(object sender, EventArgs e);
    public partial class UserControl1 : UserControl
    {
        //Your own event based on created delegate
        public event ButtonClickToMainForm ButtonClickedToMainForm;
        public UserControl1()
        {
            InitializeComponent();
        }
       //This method will invoke your event
        private void OnButtonClickedToMainForm(object sender, EventArgs e)
        {
            ButtonClickedToMainForm?.Invoke(this, e);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //On button1_Click this will fire the event on mainform
            OnButtonClickedToMainForm(this, e);
        }

和主窗体:

 public Form1()
        {
            InitializeComponent();
            //Subscribe to event from your usercontrol
            userControl11.ButtonClickedToMainForm += UserControl11_ButtonClickedToMainForm;
        }
        //Button on userControl1 has been clicked
        private void UserControl11_ButtonClickedToMainForm(object sender, EventArgs e)
        {
            //do your things here...
        }

希望这有帮助。