C# user control as a custom panel

后端 未结 2 1542
遇见更好的自我
遇见更好的自我 2021-02-15 13:02

I create my own user control which only contains a panel :

When I drag a myPanel object in the designer and then try to add a button on it, the button is in fact added t

相关标签:
2条回答
  • To be more precise, I do the following :

    [Designer(typeof(myControlDesigner))] 
    public class MyPanel : UserControl
    {
        private TableLayoutPanel tableLayoutPanel1;
        private Panel panelMain;
        ...
    
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Panel InnerPanel
        {
            get { return panelMain; }
            set { panelMain = value; }
        }
    
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public TableLayoutPanel InnerTableLayout
        {
            get { return tableLayoutPanel1; }
            set { tableLayoutPanel1 = value; }
        }
    }
    

    With myControlDesigner for [Designer(typeof(myControlDesigner))] to be

    class myControlDesigner : ParentControlDesigner
    {
    
        public override void Initialize(IComponent component)
        {
    
            base.Initialize(component);
    
            MyPanel myPanel = component as MyPanel;
            this.EnableDesignMode(myPanel.InnerPanel, "InnerPanel");
            this.EnableDesignMode(myPanel.InnerTableLayout, "InnerTableLayout");
    
        }
    
    }
    

    (Other source : Add Control to UserControl )

    0 讨论(0)
  • 2021-02-15 13:52

    Have a look here

    How to make a UserControl object acts as a control container design-time by using Visual C#

    But if you only need extended Panel functionality its better to inherit directly from Panel.

    0 讨论(0)
提交回复
热议问题