我正在開發一個c#練習項目,目的是培養我的技能和學習新東西。我創建了一個由幾個控件組成的動態界面..當窗體被加載時,它將啓動一個numericUpDown和一個Button。當用戶在數字上選擇一個數字並點擊按鈕時,它將根據numericUpDown上選擇的數字生成儘可能多的文本框,並在生成的文本框旁邊生成一個刪除按鈕。我無法刪除文本框,當用戶點擊刪除按鈕從動態界面中刪除控件
這是代碼,我有:
//生成文本框和按鈕
private void AssessmentButton_Click(object sender, EventArgs e)
{
int length = (int)this.NoAssesmentBoxlv4.Value;
for (int i = 0; i < length; i++)
{
textboxAssesmentName.Add(new TextBox());
var p = new System.Drawing.Point(110, 260 + i * 25);
(textboxAssesmentName[i] as TextBox).Location = p;
(textboxAssesmentName[i] as TextBox).Size = new System.Drawing.Size(183, 20);
this.Lv4Tab.Controls.Add(textboxAssesmentName[i] as TextBox);
buttoRemove.Add(new Button());
(buttoRemove[i] as Button).Location = new System.Drawing.Point(380, 260 + i * 25);
(buttoRemove[i] as Button).Text = @"x";
(buttoRemove[i] as Button).BackColor = Color.Red;
(buttoRemove[i] as Button).ForeColor = Color.White;
(buttoRemove[i] as Button).Size = new System.Drawing.Size(22, 23);
this.Lv4Tab.Controls.Add(buttoRemove[i] as Button);
(buttoRemove[i] as Button).Click += this.buttoRemove_click;
}
}
下面是該soruce刪除按鈕的Click(此方法不編譯)
private void buttoRemove_click(object sender, EventArgs e)
{
foreach (Object obj in textboxAssesmentName)
{
// THIS LINE DOES NOT COMPILE!!!
this.Controls.Remove(textboxAssesmentName.Remove);
}
}
任何想法,將不勝感激
*「不起作用」*是什麼意思? [我們不介意讀者。](http://meta.stackexchange.com/a/128551/102937) – 2012-04-20 22:43:15
不刪除任何控件在我的代碼 – Tacit 2012-04-20 22:55:23