如何将数据从一个窗体传递到以前从任何其他窗体实例化的另一个窗体

本文关键字:窗体 任何 实例化 另一个 其他 数据 一个 | 更新日期: 2023-09-27 18:30:22

我有三个表单(form1,form2,form3),我的主要表单是form1,从中我打开form2获取了一些数据,我在form2上有一个更新按钮,它将带我到form3,现在我希望用户在form3上写的任何内容都更新到form2,我如何使用c#.net?

(我使用 showdialog() 方法打开了 form2,form3)

//reference to form2
Form2 SecondaryForm = new Form2(mainForm);<br/>
SecondaryForm.ShowDialog();
//in the constructor of Form2 save the reference of Form1
Form1 form1 = null
Form2(Form1 mainForm)
{
    form1 = mainForm;
}
//then instead of creating a new MainForm again just use reference of Form1
form1.updateText(data);
this.Close()

我已经使用了上面的代码,但我在 form1.updateText(数据)上收到空引用异常;

如何将数据从一个窗体传递到以前从任何其他窗体实例化的另一个窗体

我刚刚尝试了这个。 我创建了两个表单,每个表单都有一个buttontextbox。在Form1

    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 SecondaryForm = new Form2(this);
        SecondaryForm.ShowDialog();
    }
    public void updateText(string txt)
    {
        textBox1.Text = txt;
    }

然后在Form2

    Form1 form1 = null;
    public Form2(Form1 mainForm)
    {
        form1 = mainForm;
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        form1.updateText(textBox1.Text);
        this.Close();
    }

我用了这个,它有效,我没有例外

只需将表单 2 引用传递给表单 3,同时实例化它.. 类似于您在打开 form2 时对 form1 所做的那样。 然后从 form3 使用 form2 引用调用 updateText 方法,该方法应该是 form2 上的公共方法

这是所有 3 个表单的代码,您可以更新其他人的任何表单,我已经做到了,以便您可以在 Form1 中访问 Form3 和 Form2。

using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public  Form2 frm2;
        public  Form3 frm3;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        public void updateText()
        {
            this.textBox1.Text = "";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (frm2 == null)
                frm2 = new Form2(this);
                frm2.ShowDialog();
        }
    }
}

using System;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form1 refToForm1;
        public Form2(Form1 f1)
        {
            refToForm1 = f1;
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (refToForm1.frm3 == null)
                refToForm1.frm3 = new Form3(this);
            refToForm1.frm3.ShowDialog();
        }
        public void UpdateForm2(string txt)
    {
        this.textBox1.Text = txt;
    }
    }
}
using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        Form2 refToForm2;
        public Form3( Form2 f2)
        {
            refToForm2 = f2;
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Pass any data to Form1;
            refToForm2.refToForm1.updateText();
            //Pass data to form2
            refToForm2.UpdateForm2("from form3");
        }
    }
}

尽管它可能不适合您的数据,但也许您可以将表单视为表示所有数据的基础"模型"的"视图"?如果是这样,您可以创建"Model"类的实例,并为所有 3 个表单提供对它的引用。有关"视图"和"模型"的一些解释,请参阅此处。