I have one MDI Form, With two Child Form named MDIParent1,Form1,Form2. MDI will Load with showing/loading this two child form in it.
private void MDIParent1_Load(object sender, EventArgs e)
{
Form childForm1 = new Form1();
childForm1.MdiParent = this;
childForm1.Show();
Form childForm2 = new Form2();
childForm2.MdiParent = this;
childForm2.Show();
}
In Form1 there is a textbox1 and a Button. And in form2 there is textbox2. So what i am trying to is when I write sometext in Form1's Textbox1 and then Click on Form1's Button, That same text will be write in Form2's Textbox2.
I Tried a lot. But Dosnt get output. the values are passing through one child form to other. but Textbox.text property is not updating.
I tried it Without MDI Form. The Form1 And Form2 will opened independently. and I have to close and reopen Form2 each and every time when i click on Form1's Button. It Works little bit. But I need it in MDI Form. While Both Child Forms were opened in MDI and then I want to Update property of textbox (In short I need to do this form2.textbox.text = Form1.textbox.text where Form1 and Form2 both are Child forms)
Regards, Salil
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.
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));
}
}
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);
}
}
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...