C# 如何从其他表单设置webbrowser documenttext?

C# 如何从其他表单设置webbrowser documenttext?,c#,winforms,C#,Winforms,我创建了一个简单的消息框来获取用户输入,并将结果设置到上一个表单的webbrowser中。 这是我的MsgInput源代码 我没有在第一个表单中添加任何内容,只是将webbrowser修改器设置为public。 当我调试的时候。尝试提交文本时返回null 主要形式 我假设您得到一个空引用异常。如果您发布主表单代码,它将非常有用。据我所知,您应该传递主形式引用,这就是为什么您会得到空引用 在代码中,更改构造函数,如下所示:ParentForm is无论父窗体的类名是什么 MsgInput frm

我创建了一个简单的消息框来获取用户输入,并将结果设置到上一个表单的webbrowser中。 这是我的MsgInput源代码

我没有在第一个表单中添加任何内容,只是将webbrowser修改器设置为public。 当我调试的时候。尝试提交文本时返回null 主要形式


我假设您得到一个空引用异常。如果您发布主表单代码,它将非常有用。据我所知,您应该传递主形式引用,这就是为什么您会得到空引用

在代码中,更改构造函数,如下所示:ParentForm is无论父窗体的类名是什么

MsgInput frm = new MsgInput(this);
            frm.input_type = "echo";
            frm.Show(this);
以主要形式


否则,请共享完整代码,我们可以快速帮助

this.mainForm=mainForm;把自己分配给它。您需要mainform的实例。将其传递给MsgInput的构造函数。也不要给控件提供公共访问修改器,总有一天你会后悔的。相反,在mainform中添加一个名为SetWebBrowserText的方法并调用它,让mainform自己设置documenttext。之前我也很好奇为什么会有一个警告,说赋值给同一个变量,我认为赋值给同一个变量是没有用的。如果我更改修改器会发生什么?我已经更新了我的问题并尝试了你的解决方案。成功了!谢谢
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

    namespace EasyPHP
    {
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void echoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var msg = new MsgInput();
            msg.input_type = "echo";
            msg.Show();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            webBrowser1.DocumentText = "<pre>";
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }
    }
}
public MsgInput(ParentForm mainForm)
        {
            this.mainForm = mainForm;
            InitializeComponent();
        }
MsgInput frm = new MsgInput(this);
            frm.input_type = "echo";
            frm.Show(this);