0
votes

I have written a code to dynamically create text boxes from an input of a single text boxImage to generate textbox.

When the user enters the data it should automatically generate textboxes like this....

With Images

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

6
There a bug in your code. I will edit the code and update in my answer. Check that. - New Developer
I have posted it as new answer. Please check. - New Developer

6 Answers

1
votes

Two ways to do this:

One way, would be to maintain a list of pointers to the textboxes & labels as you create them:

In your class definition, add a private list variable:

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

....

Now, as you create them, add them to the list:

//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 );
    ....

Now, when you wish to remove them:

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

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

Another way, would be to put a Panel control on your form, and add the new textboxes/labels onto that, instead of directly onto the main form. Then, do panel1.Controls.Clear() - to just clear the controls off the panel.

1
votes

Edit
Sorry there was an error in my code. this should definitely be ok. Try this. If this is also not work, Please let me know.

    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);
            }
        } 
    }
1
votes

This is the New Code

        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);
        }
    }  

If still there are issues let me know.

0
votes

You need to keep a track of the controls you have added and then call remove:

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

There are a number of ways you can keep at track:

  • Create private fields
  • Store them in a dictionary
  • Tag the controls with some unique identifier so you can find them again.

Which one you use is up to you.

0
votes

When you drag-drop controls into a form, Visual Studio under the hood creates code to add those controls. These are created similar to how you're creating your textboxes, so you will have to introduce some way of identifying which controls you consider "generated".

One way is to add those controls to a List<Control> so you keep a reference of "your" controls.

Another way is to add a Panel to which you add the generated controls, so you can use myPanel.Controls.Clear() to only clear controls added to it.

0
votes

You can use your own variable

inputTextBoxes.Add(textBoxNewInput);

to delete the textboxes

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