Textchanged event for dynamically create textbox

Discussion in 'C#' started by scothy, Aug 28, 2010.

  1. scothy

    scothy New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.

    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice