How to access parent form from child form, when using childform.showdialog()
-
Thursday, August 19, 2010 8:08 AM
I have a MDI form, From this form i am showing another form with childform.showdialog()
I need to show some messages from the childform but in the MDI form.
For this, how can i access the MDI Form.
Please help ..
All Replies
-
Thursday, August 19, 2010 9:04 AM
Lets say that Form1 in the parent, and Form2 is you child form:
On form1 from where you want to open form 2 you do:
Form2 form2 = new Form2(this); form2.Show();
On form2 you do:
public partial class Form2 : Form { Form1 form1 = new Form1(); public NovaSezona(Form1 _form1) { InitializeComponent(); form1 = _form1; } pubic vod YourMethod() { // now you can access with form1.XXX to the public methods, controls,.. on form1 } }
- Marked As Answer by Jing0Moderator Thursday, August 26, 2010 3:21 AM
-
Thursday, August 19, 2010 9:48 AM
but here i am using form2.showdialog() and i need to access the same form which called form2.showdialog() ..
Also Form1 form1 = new Form1() creates a new form object instead ..
-
Friday, August 20, 2010 5:29 AM
True, but take a look at the constructor that Mitja Bonca wrote.
It takes an Form1 as a parameter, replacing "new Form1()" with what is specified in the constructor.
If you write:
Form2 form2 = new Form2(this);
then the field _form1 would be set to the specific instance that called the new Form2.
Which means that you can use its public methods, properties etc etc by calling
this._form1.<some property or method name here>
and get values from the current running instance of Form1 which called Form2.
Hope to help :-)
(Sorry if my answer is a little bit unclear, but its friday and I almost just woke up)
Cheers
- Marked As Answer by Jing0Moderator Thursday, August 26, 2010 3:21 AM


