Winforms Designer: How to disconnect my usercontrol from the container?

I have a relatively simple setup. I have a user control that contains a bunch of components, some text fields and a list.

In the designer, I can drag and drop other controls into my usercontrol and add them to the usercontrol instance. I do not want it.

How can I explicitly say "Do not allow the addition of additional controls to this user control?"

Reproduction

+5
source share
2 answers

, . , . , , . , , , . - .

, , , , . - , .

+3

MyContainer.DisableAddControls - .

MyContainer.Controls.Add(..) , - Add() :

if(DisableAddControls)
{
    throw new DisableAddControlsException();
}

ContainerControl, ControlAdded .

myContainer.ControlAdded += myContainerControlAdded;

private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
{
    if(DisableAddControls)
    {
        throw new DisableAddControlsException();
    }
}

, ... nevermind.

0

All Articles