刪除按鈕之前,我產生我的程序按鈕,它應該通過清除它們:從動態數組
for (int i = 0; i < buttons.Length; i++)
this.Controls.Remove(buttons[i]);
但是,所有這些從上一代按鈕保持。什麼可能導致這種情況?
(下面是全功能的,在其他功能上numButton變化。)
int numButtons = 5;
Button[] buttons = new Button[10];
private void generate_buttons()
{
for (int i = 0; i < buttons.Length; i++)
{
this.Controls.Remove(buttons[i]);
}
for (int i = 0; i < numButtons; i++)
{
buttons[i] = new Button();
buttons[i].Name = "btn" + i.ToString();
buttons[i].Text = Convert.ToString(i + 1);
buttons[i].Size = new Size(40, 24);
int yOffset = 40;
int xOffset = 20 + i * 42;
buttons[i].Location = new Point(xOffset, yOffset);
buttons[i].BackColor = System.Drawing.SystemColors.Control;
buttons[i].Enabled = false;
buttons[i].Click += new EventHandler(this.clickMe);
buttons[i].Visible = true;
this.Height = yOffset + 104;
this.Width = xOffset + 75;
}
Controls.AddRange(buttons);
}
謝謝!這真的很有幫助。 – user1576628