How to transfer data between two totally unrelated Forms C#
I have 4 forms in my program. Form1 and its child form Form2 , Form3 and it's child form Form4. I want to transfer data between Form2 and Form4 which are totally unrelated. I am unable to do so please help.

Wim SturkenboomPosted Oct 12, 2014, 11:25 AM
class myDataClass
{
public string txt;
}
Madhukar KrishnaPosted Oct 12, 2014, 7:31 AM
Wim SturkenboomPosted Oct 12, 2014, 4:41 AM
When you switch from Form2 to Form4, store the data from Form2 in that instance and next read it back when you load (or init) Form4.
Below code uses 2 forms (1 and 2, not 3 and 4)
static class Program
{
///
/// data storage for transfer
///
public static myDataClass theData;
///
/// The main entry point for the application.
///
static void Main()
{
// create an object to hold the data of form1
theData = new myDataClass();
...
...
Yellow indicates the changes to a standard Program.cs file
My Form1 has one textbox textBox1 and a button to show my Form2
The button code
private void button1_Click(object sender, EventArgs e)
{
// store data for transfer
Program.theData.txt = textBox1.Text;
// create form2
Form2 frm2 = new Form2();
// and show it
frm2.ShowDialog();
// get rid of it
frm2.Dispose();
}
My Form2 has one textbox textBox2 that will show the data. It also has a load event.
The load event code
private void Form2_Load(object sender, EventArgs e)
{
// copy data from storage to textbox
textBox2.Text = Program.theData.txt;
}
Note that there might be other ways, but this is what I would use.