设置家长控制性能和使用Controls.Add被区别()? [英] Difference between setting control Parent property and using Controls.Add()?

查看:216
本文介绍了设置家长控制性能和使用Controls.Add被区别()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更确切地说,是这些语句

  ownerControl.GroupBox1.Controls.Remove(childControl); 
ownerControl.Controls.Add(childControl);

这是等同于

  childControl.Parent = ownerControl; 


解决方案

展望反射器,它看起来像只是调用添加(当新的父非空)。在 Controls.Add被处理与把它远离老上级。因此,实际上,以下是功能上等同(当 ownerControl 不为空):

  ownerControl.Controls.Add(childControl); //注意没有删除等

  childControl.Parent = ownerControl; 



反直觉的,但快速测试表明,它的作品。


more specifically, are these statements

ownerControl.GroupBox1.Controls.Remove(childControl);
ownerControl.Controls.Add(childControl);

an equivalent to

childControl.Parent = ownerControl;

解决方案

Looking in reflector, it looks like Parent just calls Add (when the new parent is non-null). The Controls.Add deals with taking it away from the old parent. So actually, the following are functionally equivalent (when ownerControl is not null):

ownerControl.Controls.Add(childControl); // note no Remove etc

and:

childControl.Parent = ownerControl;

Counter-intuitive, but a quick test shows that it works.

这篇关于设置家长控制性能和使用Controls.Add被区别()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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