Search code examples
c#arrayseventstextboxtextchanged

array of textboxes with the same event


I'm working on a windows form application project with c# and I’m trying to make an array of TextBox with the same event action. I mean N TextBoxes are required (N is different as user assign) while all the “TextBox_TextChanged “ event are same. I would appreciated it if you would help me.


Solution

  • Please try this.

        private void frmMain_Load(object sender, EventArgs e)
        {
               int userPermittedCount = 4 // You can add user defined permission no : of text box count here;
               int pointX = 30;
               int pointY = 40;
    
               for (int i = 0; i < userPermittedCount; i++)
               {
                  TextBox txtBox = new TextBox();
                  txtBox.Location = new Point(pointX, pointY);
                  this.Controls.Add(txtBox);
                  this.Show();
                  pointY += 20;
                  txtBox.TextChanged += txtAdd_TextChanged;
                }
       }
    
     private void txtAdd_TextChanged(object sender, EventArgs e)
     {
     }