I have written a code to dynamically create text boxes from an input of a single text box
.
When the user enters the data it should automatically generate textboxes like this….

I have used this code
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);
}
}
}
It works good but i want to update that text box if the user changes value in text box it should change dynamically. But its not happening
I also tried else condition with
else
{
MessageBox.Show("Enter Value");
this.Controls.Clear();
this.Controls.Clear();
}
But it deletes all the values in this form.
How can i delete only generated textboxes
UPDATE
Here I made changes as per a idea of @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);
}
}
}
and also added
List<TextBox> inputTextBoxes = new List<TextBox>();
List<Label> inputlabels = new List<Label>();
Here its working but the value changes each time
This is the New Code
If still there are issues let me know.