日期:2014-05-18  浏览次数:21133 次

关于 Parent的问题
我在TabControl中的Tabpage里加了一个控件
我在控件中使用了:
                        TabPage   tp   =   new   TabPage();
                        tp.Controls.Add(new   test1());
                        MessageBox.Show(this.Parent.Name);
                        this.Parent.Controls.Add(tp);
其中test1也是一个控件
我想让test1也在TabPage中
为什么总报异常?:未将对象的引用设置到对象的实例
加不上去

------解决方案--------------------
你的this.Parent是什么?应该是this.Parent为null了。
------解决方案--------------------
this就是一个form了,哪里还有parent!!
------解决方案--------------------
TabPage tp = new TabPage();
tp.Controls.Add(new test1());
MessageBox.Show(this.Parent.Name);
因为tp是New出来的,所以这个时候可能Parent是Null值,所以不能取其Name属性值。
把 MessageBox.Show(this.Parent.Name);去掉。
------解决方案--------------------
TabPage tp = new TabPage();
tp.Controls.Add(new test1());
MessageBox.Show(this.Parent.Name);
this.Parent.Controls.Add(tp);