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

Tot*_*oto 8 c# user-controls winforms

我创建了自己的用户控件,它只包含一个面板:

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

我必须设置一个属性/属性才能执行此操作,另一种方法可以执行我想要的操作吗?

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

gsh*_*arp 8

看看这里

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

但是如果你只需要扩展的Panel功能,那么最好直接从Panel继承.