2012-04-20 86 views
1

我正在開發一個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); 
    } 
} 

任何想法,將不勝感激

+2

*「不起作用」*是什麼意思? [我們不介意讀者。](http://meta.stackexchange.com/a/128551/102937) – 2012-04-20 22:43:15

+0

不刪除任何控件在我的代碼 – Tacit 2012-04-20 22:55:23

回答

0

我建議你使用ButtonTag屬性來存儲與它相關的TextBox。把它放在你的代碼,在你的循環插入此行:

(buttoRemove[i] as Control).Tag = textboxAssesmentName[i]; 

那麼你的事件處理程序是這樣的:

private void buttoRemove_click(object sender, EventArgs e) 
{ 
    this.Controls.Remove((sender as Control).Tag as Control); 
} 

編輯:這是我會寫代碼的方式(不包括錯別字)。

private void AssessmentButton_Click(object sender, EventArgs e) 
{ 
    int length = (int)this.NoAssesmentBoxlv4.Value; 
    for (int i = 0; i < length; i++) 
    { 
     TextBox t = new TextBox(); 
     System.Drawing.Point p = new System.Drawing.Point(110, 260 + i * 25);      
     t.Location = p; 
     t.Size = new System.Drawing.Size(183, 20); 

     Button b = new Button(); 
     b.Location = new System.Drawing.Point(380, 260 + i * 25); 
     b.Text = @"x"; 
     b.BackColor = Color.Red; 
     b.ForeColor = Color.White; 
     b.Size = new System.Drawing.Size(22, 23); 
     b.Click += new System.EventHandler(this.buttoRemove_click); 

     this.Lv4Tab.Controls.Add(t); 
     this.Lv4Tab.Controls.Add(b); 
     textboxAssesmentName.Add(t); 
     buttoRemove.Add(b); 
    } 
} 

private void buttoRemove_click(object sender, EventArgs e) 
{ 
    Control b = sender as Control; 
    Control t = b.Tag as Control; 
    this.Lv4Tab.Controls.Remove(t); 
    this.Lv4Tab.Controls.Remove(b); 
    textboxAssesmentName.Remove(t); 
    buttoRemove.Remove(b); 
} 
+0

中用紅色下劃線,這隻會刪除刪除按鈕,如果我點擊生成按鈕兩次,它保持生成的副本,所以ID必須點擊刪除按鈕兩次刪除它 – Tacit 2012-04-20 23:46:43