2012-11-30 22 views
0

我想知道如何刪除動態創建的textboxeslabels,我想從此事件cmbMethodActions_SelectedIndexChanged中刪除所有動態創建的textboxeslabels。因爲所有textboxeslabels都基於數據庫。每個事件都會改變,textboxeslabels也會改變。如何在C#Windows窗體中刪除動態創建的文本框和標籤?

截至目前,這裏是我的代碼:

private void cmbMethodActions_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     GetActionFields(int.Parse(cmbMethodActions.SelectedValue.ToString())); 
    } 

    private void GetActionFields(int i) 
    { 
     MySqlCommand cmd = new MySqlCommand("call GetActionField("+i+")", cn); 
     MySqlDataAdapter adapter = new MySqlDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     adapter.Fill(dt); 

     for (int x = 0; x < dt.Rows.Count; x++) 
     { 
      TextBox txtfields = new TextBox(); 
      txtfields.Name = dt.Rows[x]["field_name"].ToString(); 
      txtfields.Width = 175; 
      flowLayoutPanelText.Controls.Add(txtfields); 

      Label txtlbl = new Label(); 
      txtlbl.Name = dt.Rows[x]["field_name"].ToString(); 
      txtlbl.Text = txtlbl.Name; 
      flowLayoutPanelLabel.Controls.Add(txtlbl); 
     } 
    } 

回答

3
flowLayoutPanelText.Controls.Clear(); 
flowLayoutPanelLabel.Controls.Clear(); 
相關問題