如何从形式到另一种形式是$ P $其他任何形式的pviously实例传递数据?形式、实例、数据、pviously

2023-09-04 05:51:15 作者:猛虎隱身山

我有三种形式(Form 1中,窗口2,form3),我的主要形式是Form1上,从我打开窗口2服用一些数据,我有窗口2,将带我去form3更新按钮,现在我想,无论用户在fo​​rm3写更新为窗口2,我怎样才能使人们有可能使用C#.NET?

I have three forms(form1,form2,form3), my main form is form1, from that i opened form2 taking some data and i have an update button on form2 that will take me to form3, now i want that whatever user writes on form3 is updated to form2, how can i make it possible using c#.net?

(我使用的ShowDialog()方法中打开的窗口2,form3)

(i opened form2,form3 using showdialog() method)

//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()

我已经使用了超过code,但我是越来越nullreference异常 form1.updateText(数据);

推荐答案

只是通过形式2参照形成三个在实例它。同样像你一样为Form1,同时打开窗口2。然后从form3使用窗口2引用调用UPDATETEXT这应该是对窗口2的公共方法方法

Just pass the form 2 reference to form three while instantiating it.. similarly as you did for form1 while opening form2. then from form3 use the form2 reference to call updatetext method which should be a public method on form2

下面是code所有3种形式,您可以更新任何形式从别人,我已经说得那么您可以访问Form 1和Form中form3。

here is code for all 3 forms, you can update any form from others, i have made it so that you can access form1 and form2 in form3.

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