Winforms Designer: How to disable my usercontrol from being a container?
Asked Answered
M

2

6

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

Misread answered 13/8, 2012 at 15:13 Comment(3)
Just a question: why do you want your container control not to act as a container?Solis
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.Misread
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?Rim
M
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.

Microspore answered 13/8, 2012 at 15:52 Comment(4)
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.Misread
I added a screenshot in the main post.Misread
I have no idea how to reverse-engineer code from a screenshot like that.Microspore
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.Misread
S
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.

Solis answered 13/8, 2012 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.