我在winforms中有以下程序:
我想清除您在面板中看到的两个按钮。
我的代码似乎只清除了最后一个面板中的最后一个按钮。我需要清除所有面板。我在这里错过了什么:
public Form1()
{
InitializeComponent();
}
Panel pnl1;
Panel pnl2;
Button btn;
private void createPanels(int spacing)
{
pnl1 = new Panel();
pnl1.Location = new Point(5, spacing);
pnl1.BorderStyle = BorderStyle.FixedSingle;
pnl1.Size = new Size(85, 93);
this.Controls.Add(pnl1);
pnl2 = new Panel();
pnl2.Location = new Point(91, spacing);
pnl2.BorderStyle = BorderStyle.FixedSingle;
pnl2.Size = new Size(85, 93);
this.Controls.Add(pnl2);
}
void addCons()
{
btn = new Button();
btn.Size = new Size(40, 20);
btn.Location = new Point(5, 5);
}
private void Form1_Load(object sender, EventArgs e)
{
addCons();
createPanels(5);
pnl1.Controls.Add(btn);
addCons();
createPanels(99);
pnl1.Controls.Add(btn);
}
private void button1_Click(object sender, EventArgs e)
{
pnl1.Controls.Clear();
}