Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Form1 f1=new Form1();
f1 is the main form
f1 is an Mdi Container;

Form2 f2=new Form2();
f2.MdiParent=this;
f2.show();

Now, in constructor of the f2 I have:

Form1 f1=(Form1)this.MdiParent; // ERROR

But MdiParent is NULL...

Why?

Thank you...
Posted
Updated 15-May-13 23:43pm
v2

1 solution

Because it is in the constructor. Look at your code:
C#
Form2 f2=new Form2();
f2.MdiParent=this; 
The first line of the two calls the constructor for Form2, before the second instruction is executed.

Consider moving your code from the constructor into the Form2.Load or Form2.Shown events.

[edit]Typo: "Lein" for "Line" - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Luci C 16-May-13 6:03am    
You're right...Thank you!
OriginalGriff 16-May-13 6:06am    
:laugh: You're welcome!
ductuan_itpro 12-Jan-16 23:54pm    
Good solution!
I define a global variable _parent in Form2. Set its value at Form2_Load.
Then I can use it anywhere.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900