It will depend on how you want the form1 to work:
If form1 closes as well when you press the button, then set up two properties which provide access to the TextBox content and read them from mainForm. I would suggest using form1.ShowDialog in this case, rather than form1.Show.
If form1 doesn't close, then the best way is to set up an event in form1, which mainForm handles.
form1 throws the event, and provides the data via the EventArgs:
In form1:
public partial class form1 : 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 assign 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 mainForm:
public mainForm()
{
frmChild.Change += new frmChange.ChangeHandler(Changed);
}
private void ShowChildForm()
{
frmChild fd = new frmChild();
fd.ShowDialog();
}
private void Changed(object sender, EventArgs e)
{
}
***************************************
You can pass data in this way via the EventArgs parameter:
form1:
public partial class form1 : Form
{
public event EventHandler<ChangedArgs> Changed;
private void butGo_Click(object sender, EventArgs e)
{
EventHandler ch = Changed;
if (ch != null)
{
ch(this, new ChangedArgs(tbData.Text));
}
}
}
public partial class ChangedArgs : EventArgs
{
public string strData;
public ChangedArgs(string str)
{
strData = str;
}
}
mainForm:
public partial class mainFrom : Form
{
form1 otherForm = new form1();
private void mainForm_Load(object sender, EventArgs e)
{
otherForm.Changed += new EventHandler<ChangedArgs>(Changed);
otherForm.Show();
}
private void Changed(object sender, ChangedArgs e)
{
if (e != null)
{
tbData.Text = e.strData;
}
}
}