简体   繁体   English

设置控制父属性和使用Controls.Add()之间的区别?

[英]Difference between setting control Parent property and using Controls.Add()?

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). 查看反射器,看起来像Parent只调用Add (当新父元素为非null时)。 The Controls.Add deals with taking it away from the old parent. Controls.Add处理从旧父母那里拿走它。 So actually, the following are functionally equivalent (when ownerControl is not null): 实际上,以下是功能上等效的(当ownerControl不为null时):

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

and: 和:

childControl.Parent = ownerControl;

Counter-intuitive, but a quick test shows that it works. 反直觉,但快速测试表明它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM