5

I have a relatively simple setup. I have a custom usercontrol that has a bunch of components on it, some text boxes, and a listview.

In the designer, I can drag and drop other controls into my usercontrol, and it adds them to the usercontrol instance. I don't want this.

How can I explicitly say "Don't allow additional controls to be added to this usercontrol?"

Reproduction

greggorob64
  • 2,487
  • 2
  • 27
  • 55
  • Just a question: why do you want your container control not to act as a container? – Dave New Aug 13 '12 at 15:18
  • I wasn't aware that create a usercontrol implied that I wanted a container. I just wanted to make a control that contained several other controls, that I could use repeatedly. However, once placed, I didn't want them to be added to, I want all instanced of my usercontrol to contain the same items. – greggorob64 Aug 13 '12 at 15:39
  • I can't duplicate the issue. By default, the UserControl that you place on a form will *not* act as a container. In terms of preventing your UserControl in design mode from adding controls to it: why? Isn't that how you created the control in the first place? – LarsTech Aug 13 '12 at 15:44

2 Answers2

3

That's not the way it works. When you drop your user control on a form then adding controls to it isn't supported. That requires a special designer, this answer shows what is required. Maybe it looks like the controls get added but they merely overlap your user control. Their parent is still the form.

If a programmer opens your user control class itself in the designer then, sure, he can add controls as he pleases. The only way to stop that is to not ship the source code and use the sealed keyword to prevent deriving from it.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Unfortunately, that does not seem true. I am looking at the outline view of winforms designer right now. I dragged one control in the other, and it became embedded inside. The outline view reflects this, showing the second indented into the first. In fact, im using a tablelayoutpanel to order the controls, and it doesn't allow multiple controls in the same cell. – greggorob64 Aug 13 '12 at 17:40
  • I added a screenshot in the main post. – greggorob64 Aug 13 '12 at 17:43
  • I have no idea how to reverse-engineer code from a screenshot like that. – Hans Passant Aug 13 '12 at 17:53
  • I can appreciate that. My control does implement the following interfaces though, I'm digging through the code, and its not inheriting any interfaces besides UserControl. It does implement several designer and typeconverters, but I don't think they can be related. I should probably take the time to make a small code sample to reproduce. – greggorob64 Aug 13 '12 at 17:56
0

You could create a boolean property MyContainer.DisableAddControls or something.

If your MyContainer.Controls.Add(..) is overridden, then you can throw some custom exception in that Add() method as follows:

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

If you are inheriting that method straight from ContainerControl, then you can handle the ControlAdded event and throw the exception there.

myContainer.ControlAdded += myContainerControlAdded;

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

On second thought, this won't throw out your designer at design time... nevermind.

Dave New
  • 38,496
  • 59
  • 215
  • 394