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

标签 c# user-controls

更具体地说,这些陈述是什么

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/

相关文章:

java - Windows 或 UWP 上的 Android 建议

c#泛型接口(interface)解决方案

c# - 如何避免窗体上出现大量用户控件

wpf - 如何接受焦点 C# WPF UserControl

wpf - 在 MyUserControl.xaml 中以声明方式设置 MyUserControl 的属性

linux - 如何添加将更改 Linux 中其余脚本路径的用户输入框

C# 在委托(delegate)调用中等待

c# - 如何在Windows应用商店应用程序中获取图像的一个像素(C#)

c# - 有没有办法通过数据注释来验证一个日期属性是否大于或等于另一个日期属性?

C# 用户控件自定义属性