C# 用户控件作为自定义面板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1356676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:35:01  来源:igfitidea点击:

C# user control as a custom panel

c#winformsuser-controls

提问by Toto

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 to the form's controls.

当我在设计器中拖动一个 myPanel 对象,然后尝试在其上添加一个按钮时,该按钮实际上已添加到表单的控件中。

Is there a property/attribute I must set in order to perform this, an other way to do what I want ?

是否有我必须设置的属性/属性才能执行此操作,另一种方式来做我想做的事?

public class MyPanel : UserControl
{
    private Panel panelMain;

    public MyPanel()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.panelMain = new System.Windows.Forms.Panel();
        this.SuspendLayout();
        // 
        // panelMain
        // 
        this.panelMain.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.panelMain.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panelMain.Location = new System.Drawing.Point(0, 0);
        this.panelMain.Name = "panelMain";
        this.panelMain.Size = new System.Drawing.Size(150, 150);
        this.panelMain.TabIndex = 0;
        // 
        // myPanel
        // 
        this.Controls.Add(this.panelMain);
        this.Name = "MyPanel";
        this.ResumeLayout(false);
    }
}

采纳答案by gsharp

Have a look here

看看这里

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

如何使用 Visual C# 使 UserControl 对象在设计时充当控件容器

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

但是如果您只需要扩展面板功能,最好直接从面板继承。

回答by Toto

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

使用 myControlDesigner for [Designer(typeof(myControlDesigner))]

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)

(其他来源:向 UserControl 添加控件