Try using the the MDI parent (MDIParent1) as a moderator between the 2 forms.
MDIParent1 will register to Form1's events and will modify Form2 respectively.
Modifications for Form1
Add to Form1 a public event notifying that the button was pressed. The event should also contain information about the current text in textBox1. For this, use a class deriving from EventArgs:
The EventArgs:
public class TextChangedArgs:EventArgs
{
string _text;
/// <summary>
/// Gets text .
/// </summary>
/// <value>
/// The text.
/// </value>
public string Text
{
get { return _text; }
}
public TextChangedArgs(string text)
{
this._text = text;
}
}
The Public Event:
public event EventHandler<TextChangedArgs> OnTextChanged;
The Button1 Click Event:
private void button1_Click(object sender, EventArgs e)
{
if (this.OnTextChanged != null)
{
this.OnTextChanged(this, new TextChangedArgs(this.textBox1.Text));
}
}
Modification to MDIParent1
In the following code, the modification is the registration to the event, and handling it:
void MDIParent1_Load(object sender, EventArgs e)
{
Form1 childForm1 = new Form1();
childForm1.MdiParent = this;
childForm1.OnTextChanged += childForm1_OnTextChanged;
childForm1.Show();
Form2 childForm2 = new Form2();
childForm2.MdiParent = this;
childForm2.Show();
}
void childForm1_OnTextChanged(object sender, TextChangedArgs e)
{
//just getting the Form 2 instance, you can implement it in any way you choose to (e.g. make it global...)
Form2 childForm2 = this.MdiChildren.Where(c => c is Form2).FirstOrDefault() as Form2;
if (childForm2 != null)
{
childForm2.SetText(e.Text);
}
}
Modifications to Form2
The modification is Adding a public method for setting the text:
public void SetText(string text)
{
this.textbox2.Text = text;
}
this should work for you...