更具体地说,这些陈述是什么
ownerControl.GroupBox1.Controls.Remove(childControl);
ownerControl.Controls.Add(childControl);
相当于
childControl.Parent = ownerControl;
最佳答案
在反射器中查看,看起来 Parent
只是调用了 Add
(当新父级为非空时)。 Controls.Add
处理将其从旧父项中移除的问题。所以实际上,以下内容在功能上是等效的(当 ownerControl
不为 null 时):
ownerControl.Controls.Add(childControl); // note no Remove etc
和:
childControl.Parent = ownerControl;
违反直觉,但快速测试表明它有效。
关于c# - 设置控件父属性和使用 Controls.Add() 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/961554/