James James - 1 month ago 16
C# Question

User control containing child control, deisgn mode edit child control

I have a control, within that control there is a collection of other controls (a bit like a tab control - but not a tab control).

So I have my control on a form. I can add new child controls using a custom collection designer form, no problem.

I would like to be able to add controls to the child control in design mode from the form view. At present if I select the child control and drop say, a checkbox, the checkbox gets added to the parent control not the child control and then sits over the top of all child controls.

How do I make controls that have been dropped over the child control actually get added to the child control and not it's parent in design mode? Is it an attribute that needs adding to something? Do I have to add some custom code in there to trap the control being added?

Answer

First enable the inner control designer to behave like a parent control:

[Designer(typeof(ParentControlDesigner))]
public partial class InnerControl : UserControl

Then enable design mode for inner control when it's hosted in outer control, by creating a new control designer for outer control:

[Designer(typeof(OuterControlDesigner))]
public partial class OuterControl : UserControl
{
    public OuterControl()
    {
        InitializeComponent();
    }
    public InnerControl InnerControl { get { return innerControl1; } }
}

public class OuterControlDesigner:ControlDesigner
{
    public override void Initialize(IComponent component)
    {
        base.Initialize(component);
        this.EnableDesignMode(((OuterControl)this.Control).InnerControl, "InnerControl");
    }
}
Comments