1

I have a user control that acts as a container for other controls. My container is quite simple as it contains not much more than a styled panel that contains a TableLayoutPanel to hold multiple child controls:

MyContainerControl -> Panel -> TableLayoutPanel

Now I want to enable designer support for nesting controls. A quick search reveals that this is possible:

However my control differs slightly from the controls in the example. The example controls add the child controls to a Panel, whereas my control shall add the child controls to a TableLayoutPanel (which is incidentially a subclass of Panel).

I thought this should not be much of a problem and started implementing:

[Designer(typeof(ControlListPanel.Designer))]
public partial class ControlListPanel : UserControl
{
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public TableLayoutPanel LayoutArea
    {
        get { return this.rootTableLayoutPanel; }
    }

    // ... more class content ...

    public class Designer : ParentControlDesigner
    {
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            if (this.Control is ControlListPanel)
            {
                this.EnableDesignMode(((ControlListPanel)this.Control).LayoutArea, "LayoutArea");
            }
        }
    }
}

Now I am able to drag and drop controls onto my user control, but looking at the designer file reveals the control was added to the user control itself:

this.controlListPanel1.Controls.Add(this.myChildControl);

I expected it to be added to the table layout:

this.controlListPanel1.LayoutArea.Controls.Add(this.myChildControl);

When I change the LayoutArea property as follows, the designer does what I expect and adds the child controls to my LayoutArea property:

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel LayoutArea
{
    get { return this.panel; }
}

(I return the panel instead of the table layout)

Is there something I am missing? Why does the designer not want to add my child controls to the TableLayoutPanel?

Community
  • 1
  • 1
NobodysNightmare
  • 2,992
  • 2
  • 22
  • 33
  • Putting the TLP in design mode when it is nested inside a UC is not supported. – Hans Passant Sep 02 '14 at 12:53
  • Not an exact duplicate, but the root cause is: http://stackoverflow.com/questions/22181926/controls-on-inherited-form-are-locked-can-it-be-undone – DonBoitnott Sep 02 '14 at 13:02
  • @HansPassant That is sad. Is there any workaround you would see? As far as I can tell I can decide between not having designer support and not using the TLP at all, right? – NobodysNightmare Sep 02 '14 at 13:21
  • Hm. @Hans: I wonder where's the rub with this idea: Put the TLP somewhere else, where you can style it in the designer 'til you're happy and then, at runtime(!) move it to the target panel of yours..? I tried with an extra Form, but a hidden tab should work as well..: `public Form2 F2; private void testButton_Click(object sender, EventArgs e) { F2 = new Form2(); targetPanel.Controls.Add(F2.TLP); F2.Close(); }` where F2.TLP is a public property on Form2 which the FLP gets assigned to in F2's constructor. – TaW Sep 02 '14 at 15:16
  • @TaW - the designer serializes the design to code. That code already exists in this case, locked up inside the UserControl, you can't put it "somewhere else" nor modify it. The need to distinguish old code that came from the UC from new code that needs to be added is surely the hangup with TLP. – Hans Passant Sep 02 '14 at 15:25
  • I mean: Create&design in e.g. in a Form and __at runtime__ add the __FLP__ to the target, not the code. It works fine and I can add such a __'out-of-place edited'__ FLP to some other Controls collection. Or more than one for that matter by creating more Form2 instances. – TaW Sep 02 '14 at 15:31
  • I see one rub, but it is one that is partially mendable: If the OP wants to script the controls in the TLP in the UC then he must creat properties as stand-ins which he can code and he must wire them to the Controls in the TLP once it has 'arrived'. So my idea will fix the problem of visual design but not really that of code design.. – TaW Sep 02 '14 at 15:43
  • Hm, I just tried to add code to a button in the TLP and to my amazement it works. I'm closing F2 and added a Dispose() as well but it still works. I wonder where the code is..? – TaW Sep 02 '14 at 16:11

0 Answers0