C# 如何在C中禁用向除其父容器以外的任何容器添加控件#
你好。我曾经将我的控件添加到ParentControl.Controls。如何禁用将此控件添加到除其父容器之外的任何容器 更新:C# 如何在C中禁用向除其父容器以外的任何容器添加控件#,c#,winforms,controls,C#,Winforms,Controls,你好。我曾经将我的控件添加到ParentControl.Controls。如何禁用将此控件添加到除其父容器之外的任何容器 更新: public class CompositeControl : Control { public CompositeControl(EmbeddedControl[] embeddedControls) { this.Controls.AddRange(embeddedControls); } protected ov
public class CompositeControl : Control
{
public CompositeControl(EmbeddedControl[] embeddedControls)
{
this.Controls.AddRange(embeddedControls);
}
protected override void OnPaint(PaintEventArgs e)
{
foreach (Control c in this.Controls)
{
if (c is EmbeddedControl) c.Locaion = new Point(someX, someY);
base.OnPaint(e);
}
}
}
public class EmbeddedControl : UserControl
{
}
EmbeddedControl uc1 = new EmbeddedControl(); //design by user
EmbeddedControl uc2 = new EmbeddedControl(); //design by user
EmbeddedControl[] coll = new EmbeddedControl[] {uc1, uc2};
CompositeControl compControl = new CompositeControl(coll);
Button b = new Button();
compControl.Controls.Add(b);
b.location = new Point(100, 100);
...
compControl.Controls.Remove(b); //ok
我想让它变得不可能:
Panel p = new Panel();
p.Controls.Add(uc1); //uc1 will removed from compControl.Controls
您可以创建自己的panel类,该类可以从panel中访问。如果有人通过方法添加控件,则您会在内部将其删除。我希望这就是您想要实现的目标。请向我们展示您迄今为止所做的工作,以及您希望如何使用此功能的代码示例。我更新了我的问题