Resolved Adding Controls to Custom Controls during Design Time

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
104
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");
        }
    }
}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,362
Location
Norway
Programming Experience
10+

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
104
Programming Experience
10+
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:

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,362
Location
Norway
Programming Experience
10+
Inherit your designer from ParentControlDesigner instead of ControlDesigner.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,249
Location
Sydney, Australia
Programming Experience
10+
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?
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,362
Location
Norway
Programming Experience
10+
@jmcilhinney when you add a user control to a form in designer it will not act as a container control by default.
 
Top Bottom