Ok, if you want to transfer data back from Form2 to Form1 and put it back in the place that data came from, then unless you close Form2to return the data, then your only option is to use an Event.
This isn't difficult, but it takes a little work on your part.
You need an Event in Form2 which Form1 handles, and a Property in Form2 which returns the data.
Form2:
public event EventHandler DataAvailable;
protected virtual void OnDataAvailable(EventArgs e)
{
EventHandler eh = DataAvailable;
if (eh != null)
{
eh(this, e);
}
}
void returnDataButton_Click(object sender, EventArgs e)
{
OnDataAvailable(null);
}
public string Data
{
get { return myTextBox.Text; }
set { myTextBox.Text = value; }
}
Form1:
private void showForm2Button_Click(object sender, EventArgs e)
{
Form2 form2Instance = new Form2();
form2Instance.DataAvailable += new EventHandler(form2Instance_DataAvailable);
form2Instance.ShowDialog();
}
void form2Instance_DataAvailable(object sender, EventArgs e)
{
Form2 f = sender as Form2;
if (f != null)
{
textBox2.Text = f.Data;
}
}
What the code does is create an event in Form2, that tells Form1 that new data is available - the handler then picks it up from the property, via the instance of the form that reported data was available.
(Hopefully, by now you are comfortable with instances, events and
as
syntax so it all makes sense with a little reading)