简体   繁体   中英

how to pass data from a form to another form that is previously instantiated from any other form?

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?

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

i have used the above code but i am getting nullreference exception on form1.updateText(data);

I just tried this. i created two forms, each form with a button and textbox . In 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;
    }

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

I used this and it worked and i got no exceptions

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

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

Although it may not be appropriate for your data, maybe you could consider your forms as 'Views' for an underlying 'Model' that represents all your data? If so, you can create an instance of your 'Model' class and give all 3 forms a refernce to it. See here for some explanation of 'View' and 'Model'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM