Resolved Adding Controls to Custom Controls during Design Time

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
129
Programming Experience
10+
I have a Custom WinForms UserControl. I would like to be able to drop other controls, like a CheckBox, on top of it during Design Time so that the dropped Control is now the child of my Custom Control. Any ideas on how to make this happen?

My simple Custom Control is defined as follows:

CustomeUserControl:
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace SlidingPanel
{

    [Designer(typeof(SlidePanelDesigner))]
    public partial class SlidePanel : UserControl
    {
        public SlidePanel()
        {
            InitializeComponent();
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Label HeaderLabel { get { return Header; } }
    }

    class SlidePanelDesigner : ControlDesigner
    {
        public override void Initialize(IComponent comp)
        {
            base.Initialize(comp);
            var uc = (SlidePanel)comp;
            EnableDesignMode(uc.HeaderLabel, "HeaderLabel");
        }
    }
}
 
Thanks! That work like a charm!

But since it appears I can only have one designer, I loose the ability to re-size my label at design time. Is there a way to have both?

Child Controls AND Resizable Controls:
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace SlidingPanel
{

    [Designer(typeof(SlidePanelDesigner))]
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class SlidePanel : UserControl
    {
        public SlidePanel()
        {
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Label HeaderLabel { get { return Header; } }
    }

    class SlidePanelDesigner : ControlDesigner
    {
        public override void Initialize(IComponent comp)
        {
            base.Initialize(comp);
            var uc = (SlidePanel)comp;
            EnableDesignMode(uc.HeaderLabel, "HeaderLabel");
        }
    }
}
 
Last edited:
Inherit your designer from ParentControlDesigner instead of ControlDesigner.
 
Am I missing something here? Shouldn't you just be able to use the User Control (Windows Forms) item template and away you go? Have you used a different item template and are now trying to add in the functionality that it provides for free?
 
@jmcilhinney when you add a user control to a form in designer it will not act as a container control by default.
 
Back
Top Bottom