C# 正在尝试创建自定义面板

C# 正在尝试创建自定义面板,c#,custom-panel,C#,Custom Panel,我试图创建一个自定义面板,其中将包含许多控件。这是我第一次创建这样的自定义控件。此示例包含一个面板,该面板内有一个按钮。 当我点击按钮时,我不知道为什么面板没有添加到Form1中 我的代码中遗漏了什么吗 private void按钮2\u单击(对象发送者,事件参数e) { streamingPanel sp=新streamingPanel(); sp.尺寸=新尺寸(319240); 点=新点(50,50); sp.位置=点; 此.Controls.Add(sp); } 公共类streaming

我试图创建一个自定义面板,其中将包含许多控件。这是我第一次创建这样的自定义控件。此示例包含一个面板,该面板内有一个按钮。

当我点击按钮时,我不知道为什么面板没有添加到Form1中
我的代码中遗漏了什么吗

private void按钮2\u单击(对象发送者,事件参数e)
{
streamingPanel sp=新streamingPanel();
sp.尺寸=新尺寸(319240);
点=新点(50,50);
sp.位置=点;
此.Controls.Add(sp);
}
公共类streamingPanel:面板
{
public System.Windows.Forms.Panel panel1;
public System.Windows.Forms.Button按钮1;
公共精简小组()
{
初始化组件();
}
私有void InitializeComponent()
{
this.panel1=new System.Windows.Forms.Panel();
this.button1=new System.Windows.Forms.Button();
这个.panel1.SuspendLayout();
// 
//小组1
// 
this.panel1.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.button1);
this.panel1.Location=新系统图纸点(483,91);
this.panel1.Name=“panel1”;
this.panel1.Size=新系统图纸尺寸(319240);
this.panel1.TabIndex=0;
// 
//按钮1
// 
this.button1.Location=新系统图纸点(100,72);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(75,23);
this.button1.TabIndex=0;
this.button1.Text=“button1”;
this.button1.UseVisualStyleBackColor=true;
}

}
观察将控件添加到窗体时Visual Studio editor的操作。代码位于从窗体的构造函数调用的方法
InitializeComponent
中。例如,您忘记设置新控件的
位置
大小

是的,我正在尝试遵循Visual Studio editor的操作。我在原来的帖子中修改了代码。我添加了
初始化组件
并设置了
位置
大小
,但仍然看不到面板?删除
面板1
您的自定义控件是一个
面板
,并将您为删除的
面板1
所做的一切都设置为
。并将按钮添加到
this.Controls.add(按钮1)是,这就是解决方案。现在我明白了。这当然是一个小组。我只是盲目地复制它在VisualStudio中的布局。谢谢你的帮助!非常欢迎,祝你好运。是的,谢谢!用于此操作的控件不是Panel,而是UserControl。