If you are just wanting parent to child communication then you can use the constructor to pass values. Do NOT use this method to pass from child to parent or sibling to sibling (use events instead).
For value types this works well, for reference types you have to be careful as a change to the object in the child will affect the parent(s).
using System;
using System.Windows.Forms;
namespace CPWinForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int value;
if (int.TryParse(textBox1.Text, out value))
{
Form2 form2 = new Form2(value);
form2.Show();
}
}
}
}
using System;
using System.Windows.Forms;
namespace CPWinForms
{
public partial class Form2 : Form
{
private int value1;
public Form2()
: this(0)
{ }
public Form2(int value1)
{
InitializeComponent();
this.value1 = value1;
}
private void button1_Click(object sender, EventArgs e)
{
int value;
if (int.TryParse(textBox1.Text, out value))
{
Form3 form3 = new Form3(value, value1);
form3.Show();
}
}
}
}
using System.Windows.Forms;
namespace CPWinForms
{
public partial class Form3 : Form
{
public Form3()
: this(0, 0)
{ }
public Form3(int value1, int value2)
{
InitializeComponent();
label1.Text = (value1 + value2).ToString();
}
}
}