In order to do this, one form must know about the existance of the other, which is generally a bad idea unless one opens the other directly.
If Form1 opens Form2, then there are two ways to pass information, depending on whether Form2 is displayed with Show or ShowDialog:
ShowDialog is easy: Create a public property (or several) in Form2, load the values into the property from Form1 before calling ShowDialog and fetch them back as necessary when it returns.
Show is harder - you need to create an event in Form2 which Form1 subsribes to before calling Show - you can then pass data from Form2 to Form1 either via a custom EventArgs, or via properties as above.
In the child form:
public partial class frmChild : Form
{
public event EventHandler Changed;
protected virtual void OnChanged(EventArgs e)
{
EventHandler eh = Changed;
if (eh != null)
{
eh(this, e);
}
}
private void DoSomethingToChangeData()
{
OnChanged(null);
}
}
----- The asign to eh is in case the handler changes between null check
----- and exec.
----- (unlikely, but possible)
----- The null check is to ensure there is a handler. If not, better to
----- ignore it gracefully, than to rely on the thrown exception
----- (NullReferenceException)
In the Parent form:
public frmParent()
{
frmChild.Change += new frmChange.ChangeHandler(Changed);
}
private void ShowChildForm()
{
frmChild fd = new frmChild();
fd.ShowDialog();
}
private void Changed(object sender, EventArgs e)
{
}