[英]Removing dynamically created textboxes
我已經編寫了一個代碼,可以根據單個文本框的輸入動態創建文本框 。
當用戶輸入數據時,它應該自動生成這樣的文本框。
我已經使用了這段代碼
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();
}
但是它將刪除此表單中的所有值。
我如何只刪除生成的文本框
更新在這里,我根據@New Developer的想法進行了更改
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>();
在這里它的工作,但價值每次改變
有兩種方法可以做到這一點:
一種方法是在創建文本框和標簽時維護它們的指針列表:
在類定義中,添加一個私有列表變量:
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
上放置一個Panel
控件,然后在其上添加新的文本框/標簽,而不是直接在主窗體上。 然后,執行panel1.Controls.Clear()
-清除面板上的控件。
編輯
抱歉,我的代碼中有錯誤。 這絕對可以。 嘗試這個。 如果這還是行不通的,請告訴我。
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);
}
}
}
這是新密碼
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);
}
}
如果仍然有問題,請告訴我。
您需要跟蹤已添加的控件,然后調用remove:
this.Controls.Remove(labelInput);
this.Controls.Remove(textBoxNewInput);
您可以通過多種方式進行跟蹤:
您使用哪一個取決於您。
當您將控件拖放到窗體中時,引擎蓋下的Visual Studio會創建代碼以添加這些控件。 它們的創建方式與創建文本框的方式類似,因此您將必須采用某種方式來識別您認為“已生成”的控件。
一種方法是將這些控件添加到List<Control>
以便保留對“您的”控件的引用。
另一種方法是添加一個Panel
在其中添加生成的控件,因此可以使用myPanel.Controls.Clear()
僅清除添加到其中的控件。
您可以使用自己的變量
inputTextBoxes.Add(textBoxNewInput);
刪除文本框
else
{
MessageBox.Show("Enter Value");
this.Controls.Remove(inputTextBoxes[YourIndex/NameOfControl]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.