2012-12-31 151 views
0

我已經編寫了一個代碼,用於從單個文本框Image to generate textbox的輸入中動態創建文本框。刪除動態創建的文本框

當用戶輸入數據就應自動生成這樣的文本框....

With Images

我已經使用這個代碼

private void textBoxInput_TextChanged(object sender, EventArgs e) 
    { 
     if (!string.IsNullOrEmpty(textBoxInput.Text)) 
     { 
      //Get the number of input text boxes to generate 
      int inputNumber = Int32.Parse(textBoxInput.Text); 

      //Initialize list of input text boxes 
      inputTextBoxes = new List<TextBox>(); 

      //Generate labels and text boxes 
      for (int i = 1; i <= inputNumber; i++) 
      { 
       //Create a new label and text box 
       Label labelInput = new Label(); 
       TextBox textBoxNewInput = new TextBox(); 

       //Initialize label's property 
       labelInput.Text = "Product" + i; 
       labelInput.Location = new Point(30, textBoxInput.Bottom + (i * 30)); 
       labelInput.AutoSize = true; 

       //Initialize textBoxes Property 
       textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3); 

       //Add the newly created text box to the list of input text boxes 
       inputTextBoxes.Add(textBoxNewInput); 

       //Add the labels and text box to the form 
       this.Controls.Add(labelInput); 
       this.Controls.Add(textBoxNewInput); 
      } 


     } 
} 

它的工作不錯,但我想更新該文本框,如果用戶改變文本框中的值,它應該動態改變。但它不是發生

我也試過其他條件與

else 
     { 
      MessageBox.Show("Enter Value"); 

      this.Controls.Clear(); 
      this.Controls.Clear(); 

     } 

但它刪除此表中的所有值。

我怎麼能只刪除生成的文本框

UPDATE 我在這裏所做的更改按@New開發

if (!string.IsNullOrEmpty(textBoxInput.Text)) 
     { 
      //Get the number of input text boxes to generate 
      int inputNumber = Int32.Parse(textBoxInput.Text); 

      if (inputTextBoxes != null && inputTextBoxes.Count > inputNumber) 
      { 
       int removecount = inputTextBoxes.Count - inputNumber; 

       for (int i = 0; i < removecount; i++) 
       { 
        TextBox t = inputTextBoxes[inputTextBoxes.Count - 1]; 
        inputTextBoxes.RemoveAt(inputTextBoxes.Count - 1); 
        t.Dispose(); 
       } 

       return; 
      } 


      if (inputlabels != null && inputlabels.Count > inputNumber) 
      { 
       int removecount2 = inputlabels.Count - inputNumber; 

       for (int i = 0; i < removecount2; i++) 
       { 
        Label l = inputlabels[inputlabels.Count - 1]; 
        inputlabels.RemoveAt(inputlabels.Count - 1); 
        l.Dispose(); 
       } 

       return; 
      } 

      //Generate labels and text boxes 
      for (int i = 1; i <= inputNumber; i++) 
      { 
       //Create a new label and text box 
       Label labelInput = new Label(); 
       TextBox textBoxNewInput = new TextBox(); 

       //Initialize label's property 
       labelInput.Text = "Product" + i; 
       labelInput.Location = new Point(30, textBoxInput.Bottom + (i * 30)); 
       labelInput.AutoSize = true; 

       //Initialize textBoxes Property 
       textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3); 

       //Add the newly created text box to the list of input text boxes 
       inputTextBoxes.Add(textBoxNewInput); 
       inputlabels.Add(labelInput); 

       //Add the labels and text box to the form 
       this.Controls.Add(labelInput); 
       this.Controls.Add(textBoxNewInput); 
      } 
     } 
    } 

的想法,還增加了

List<TextBox> inputTextBoxes = new List<TextBox>(); 
    List<Label> inputlabels = new List<Label>(); 

這裏的工作但價值每次都在變化

+0

代碼中存在一個錯誤。我將編輯代碼並在我的答案中更新。檢查一下。 –

+0

好的朋友謝謝 –

+0

我已經發布它作爲新的答案。請檢查。 –

回答

1

這是新代碼

 if (!string.IsNullOrEmpty(textBoxInput.Text)) 
     { 
      //Get the number of input text boxes to generate 
      int inputNumber = Int32.Parse(textBoxInput.Text); 

      if (inputTextBoxes != null && inputTextBoxes.Count > inputNumber) 
      { 
       int removecount = inputTextBoxes.Count - inputNumber; 

       for (int i = 0; i < removecount; i++) 
       { 
        TextBox t = inputTextBoxes[inputTextBoxes.Count - 1]; 
        inputTextBoxes.RemoveAt(inputTextBoxes.Count - 1); 
        t.Dispose(); 

        Label l = inputlabels[inputlabels.Count - 1]; 
        inputlabels.RemoveAt(inputlabels.Count - 1); 
        l.Dispose(); 
       } 

       return; 
      } 

     //Generate labels and text boxes 
     for (int i = 1; i <= inputNumber; i++) 
     { 
      //Create a new label and text box 
      Label labelInput = new Label(); 
      TextBox textBoxNewInput = new TextBox(); 

      //Initialize label's property 
      labelInput.Text = "Product" + i; 
      labelInput.Location = new Point(30, textBoxInput.Bottom + (i * 30)); 
      labelInput.AutoSize = true; 

      //Initialize textBoxes Property 
      textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3); 

      //Add the newly created text box to the list of input text boxes 
      inputTextBoxes.Add(textBoxNewInput); 
      inputlabels.Add(labelInput); 

      //Add the labels and text box to the form 
      this.Controls.Add(labelInput); 
      this.Controls.Add(textBoxNewInput); 
     } 
    } 

如果還是有問題,讓我知道。

+0

好朋友有沒有辦法刪除所有的文本框和標籤,如果有在輸入框中沒有值? –

+0

循環訪問**列表**,然後訪問測試框。檢查文本框的值** if(t.text == null)**。如果爲null,則刪除文本框。 –

0

你需要保持你所添加的控制的軌道,然後調用刪除:

this.Controls.Remove(labelInput); 
this.Controls.Remove(textBoxNewInput); 

有許多的方法可以保持在跟蹤:

  • 創建私有字段
  • 將它們存儲在字典中
  • 用一些唯一標識符標記控件,以便您可以再次找到它們。

你使用哪一個取決於你。

+0

這些代碼在這裏不工作朋友 –

0

將控件拖放到表單時,引擎蓋下的Visual Studio會創建代碼以添加這些控件。這些創建類似於您創建文本框的方式,因此您將不得不引入一些方法來確定您認爲哪些控件是「生成的」。

一種方法是將這些控件添加到List<Control>,以便保留「您的」控件的引用。

另一種方法是增加一個Panel向其中添加生成的控制,所以你可以使用myPanel.Controls.Clear()來加入到它唯一明確的控制。

+0

我認爲面板將是一個最好的方式,但我希望我將有一個面板或任何其他人在我的形式 –

0

你可以使用你自己的變量

inputTextBoxes.Add(textBoxNewInput); 

刪除文本框

else 
{ 
    MessageBox.Show("Enter Value"); 
    this.Controls.Remove(inputTextBoxes[YourIndex/NameOfControl]); 
} 
+0

我試過這個,但它顯示錯誤inputTextBoxes ..... –

0

兩種方法可以做到這一點:

的一種方式,是保持指針到列表文本框&標籤爲您創建它們:

在您的類d efinition,添加一個私有列表變量:

public partial class Form1 : Form 
{ 
    private List<TextBox> generatedTextboxes = new List<TextBox>(); 
    private List<Label> generatedLabels = new List<Label>(); 

.... 

現在,當您創建它們,將它們添加到列表:

//Generate labels and text boxes 
for (int i = 1; i <= inputNumber; i++) 
{ 
    //Create a new label and text box 
    Label labelInput = new Label(); 
    TextBox textBoxNewInput = new TextBox(); 
    //Keep track of the references 
    generatedTextboxes.Add(textBoxNewInput); 
    generatedLabels.Add(labelInput); 
    .... 

現在,當你想刪除它們:

for (int i = 1; i <= generatedTextboxes.Count; i++) 
{ 
    this.Controls.Remove(generatedTextboxes[i]); 
    this.Controls.Remove(generatedLabels[i]); 
} 

generatedTextboxes.Clear(); 
generatedLabels.Clear(); 

另一種方法是,將Panel控件放在窗體上,並將新的文本框/標籤添加到該窗體上,而不是直接添加到主窗體上。然後,執行panel1.Controls.Clear() - 清除面板上的控件。

+0

即使這段代碼也不工作的朋友,我不想添加面板,我希望上述方法將是相當有用的... –

1

編輯
對不起,我的代碼有錯誤。這應該是確定的。嘗試這個。如果這也不起作用,請讓我知道。

List<TextBox> inputTextBoxes = new List<TextBox>(); 

    private void textBoxInput_TextChanged(object sender, EventArgs e) 
    { 
     if (!string.IsNullOrEmpty(textBoxInput.Text)) 
     { 
      //Get the number of input text boxes to generate 
      int inputNumber = Int32.Parse(textBoxInput.Text); 
      if (inputTextBoxes != null && inputTextBoxes.Count > inputNumber) 
      { 
       int removecount = inputTextBoxes.Count - inputNumber; 

       for (int i = 0; i < removecount; i++) 
       { 
        TextBox t = inputTextBoxes[inputTextBoxes.Count-1]; 
        inputTextBoxes.RemoveAt(inputTextBoxes.Count - 1); 
        t.Dispose(); 
       } 

       return; 
      }     

      //Generate labels and text boxes 
      for (int i = 1; i <= inputNumber; i++) 
      { 
       //Create a new label and text box 
       Label labelInput = new Label(); 
       TextBox textBoxNewInput = new TextBox(); 

       //Initialize label's property 
       labelInput.Text = "Product" + i; 
       labelInput.Location = new Point(30, textBoxInput.Bottom + (i * 30)); 
       labelInput.AutoSize = true; 

       //Initialize textBoxes Property 
       textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3); 

       //Add the newly created text box to the list of input text boxes 
       inputTextBoxes.Add(textBoxNewInput); 

       //Add the labels and text box to the form 
       this.Controls.Add(labelInput); 
       this.Controls.Add(textBoxNewInput); 
      } 
     } 
    } 
+0

不工作的朋友.... –

+0

我試過這段代碼。它對我來說工作得很好。你應該做的就是確定你需要刪除多少個文本字段。不要在** textBoxInput_TextChanged **中初始化** inputTextBoxes **。它應該在全局變量之類的函數外面。你能否提供更多的細節爲什麼代碼不起作用。所以我可以幫助你。 –

+0

我已更新我的答案。請檢查一下。 –