Feofilakt
Feofilakt

Reputation: 1387

How to disable adding of control to any container except its parent in C#

Good day. I one time add my control to ParentControl.Controls. How to disable adding of this control to any container except its parent?

UPDATE:

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

I want to make it impossible:

Panel p = new Panel();
p.Controls.Add(uc1); //uc1 will removed from compControl.Controls

Upvotes: 0

Views: 106

Answers (1)

Sebi
Sebi

Reputation: 3979

You can make your own panel class which inerhits from Panel. If someone add a control via the method you delete it internaly. I hope this is what you want to achieve.

Upvotes: 1

Related Questions