Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9077369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:19:27+00:00 2026-06-16T19:19:27+00:00

I have written a code to dynamically create text boxes from an input of

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:19:29+00:00Added an answer on June 16, 2026 at 7:19 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the code to create text field dynamically. It should create element
I have written a code for taking JSON data from a PHP and putting
I have written this code inside a servlet to delete certain records from three
To dynamically fill DropDown controls on my HTML Form, I have written code that
I have written a piece of code in C#. That is, dynamically adding TextBox
This is the code i have written to dynamically generate Controls: private void GenCtrl(string
I have written a CGI script that creates an image dynamically using GET data.
i'm trying to create dynamically generated icon list for tiny mce. I have written
Hello there i have written some code in jquery to add new input type
I have written code that opens 16 figures at once. Currently, they all open

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.