Depends on how you show your forms.
If you use ShowDialog then it is easy: just create a public property in the child form, and access it directly in the parent:
ChildForm child = new ChildForm();
child.MyProperty = "Hello";
child.ShowDialog();
Console.WriteLine(child.MyProperty);
If you use Show instead then it is slightly harder - you have to create property and an event, or an event and a custom EventArgs
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:
private void ShowChildForm()
{
frmChild fd = new frmChild();
fd.Changed += new EventHandler(Changed);
fd.Show();
}
private void Changed(object sender, EventArgs e)
{
frmChild fc = sender as frmChild;
if (fc != null)
{
Console.WriteLine(fc.MyProperty);
}
}