为什么MdiParent为null? [英] Why is MdiParent null?

查看:166
本文介绍了为什么MdiParent为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Form1 f1 = new Form1();

f1是主要表格

f1是Mdi容器;



Form2 f2 = new Form2();

f2.MdiParent = this;

f2.show();



现在,在f2的构造函数中我有:



Form1 f1 =(Form1)this.MdiParent; //错误



但是MdiParent是NULL ...



为什么?



谢谢...

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...

推荐答案

因为它在构造函数中。看看你的代码:

Because it is in the constructor. Look at your code:
Form2 f2=new Form2();
f2.MdiParent=this; 

在执行第二条指令之前,两行的第一行调用Form2的构造函数。



考虑将代码从构造函数移入Form2.Load或Form2.Shown事件。



错字:Lein代表Line - OriginalGriff [/ edit]

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]


这篇关于为什么MdiParent为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆