Hi, I am newbie to developing a windows application using C#.

In that index page, when a first textbox text changed, it should automatically add a new field below the first textbox. And if the second textbox ( child control)text changed, it should automatically add a new field below the second textbox. This will go until the last text field doesn't change.
The textbox created so far, but i cannot set the position for the child controls...its merged.. Have to give the dynamic X,Y points but my code not crossing the level.

I tried the below code.

public partial class Form1 : Form
        int x = 8;
        int y = 8;

        public Form1()

        private void Form1_Load(object sender, EventArgs e)

            TextBox txtBox = new TextBox();
            txtBox.Name = "TextBoxName";
            txtBox.Location = new Point(x, y);

            txtBox.TextChanged += new EventHandler(this.TextBox_TextChanged);


        private void TextBox_TextChanged(object sender, System.EventArgs e)
            int n = 2;
            for (int i = 1; i < n; i++)
                TextBox tb = new TextBox();
                tb.Location = new System.Drawing.Point(8, 8 + i * 20);
                tb.Name = "TextBoxName" + i.ToString();
                tb.Size = new System.Drawing.Size(184, 20);
                tb.TabIndex = i + 2;                


        private void IterateThroughChildren(Control parentControl)
            foreach (Control childControl in parentControl.Controls)
                // Find a Particular Control by its Type
                if (childControl.GetType().ToString().Equals("System.Windows.Forms.TextBox"))
                    childControl.TextChanged += new EventHandler(this.TextBox_TextChanged);
                if (childControl.HasChildren)

Any help would be most appreciated.


Edited by scothy: n/a

7 Years
Discussion Span
Last Post by scothy
private void TextBox_TextChanged(object sender, System.EventArgs e)
   TextBox oldTextBox = (sender as TextBox);
   TextBox newTextBox = new TextBox();
   newTextBox.Location = new Point(oldTextBox.Location.X, oldTextBox.Location.Y + oldTextBox.Height +5);
   newTextBox.Size = oldTextBox.Size;
   newTextBox.TextChanged += new EventHandler(TextBox_TextChanged);


Is that possible to assign different ID/Name for all the dynamic & parent controls?

and can anybody help me to do this?


You can use an integer counter appended to a string to generate unique names. Something like:

public partial class frmDynamic : Form

int ID = 0;

private void AddTextbox()
    TextBox t = new TextBox();
    t.Name = "TextBox" + ID.ToString(); //TextBox0, TextBox1, etc
    ID++; //increment counter

Edited by Geekitygeek: n/a


I've just had a look at your code in the original post and it seems you are already doing this so i may have misunderstood your question.
Can you clarify what it is you want to do?


Hi all,

As per my original post, i have created the dynamic controls after the text changed event fired. But now i need to do some calculations on that. So i thought of giving identifier to each controls is easier to do calculation on that.

Eg: If quantity increases(qty textbox) the relevant price(price textbox) should be multiply by quantity.

Note : All the textboxes are dynamic controls.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.