我想創建Button
的動態的動態的數量通過給範圍在TextBox
。如何刪除動態創建的按鈕
問題是,當我輸入範圍例如(3)。它會創建3個按鈕,但是當我給出的範圍小於例如(2)之前給出的範圍時。它不顯示2個按鈕,它向我顯示前3個按鈕。我的代碼適用於大於先前範圍的範圍,但在新範圍小於上一範圍時失敗。
這裏是我的代碼:
private void button2_Click(object sender, EventArgs e)
{
int number = int.Parse(textBox3.Text);
Button[] textBoxes = new Button[number];
int location = 136;
for (int i = 0; i < textBoxes.Length; i++)
{
location += 81;
var txt = new Button();
textBoxes[i] = txt;
txt.Name = "text" + i.ToString();
txt.Text = "textBox" + i.ToString();
txt.Location = new Point(location, 124);
txt.Visible = true;
this.Controls.Add(txt);
}
}
爲什麼你給'Button'數組命名如'textBoxes'? –
首先我想創建文本框。所以我忘了改名字。 – Loyal