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()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

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

            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;                
                this.panel1.Controls.Add(tb);

                IterateThroughChildren(this);               
            }
        }

        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)
                {
                    IterateThroughChildren(childControl);
                }
            }
        }    
    }
}

Any help would be most appreciated.

Thanks

Recommended Answers

All 6 Replies

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);
   this.panel1.Controls.Add(newTextBox);
}

Hi samil,

Thanks its working great...

Hi,

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
    this.Controls.Add(t);
    ID++; //increment counter
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.