adding Textboxes dynamically into a winform

  • Thread starter Thread starter Shayer
  • Start date Start date
S

Shayer

Hello

I want to add some dynamic control in the win form when i press a button
I write the following line of code. But the problem is instead of displaying
10 textbox it display 9 textboxes.But it display another textbox when i hit
the button again. Can any one pls give me any solution of it. Anyother
alternative solution is appriciable as well.

Thanks

private void btnProcessNow_Click(object sender, System.EventArgs e)

{

int x=750;

int y=300;

for(int i=0;i<10;i++)

{

this.SuspendLayout();

this.grpFieldPanel.SuspendLayout();

TextBox a=new TextBox();

a.Anchor = (System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Right);

a.Location= new System.Drawing.Point(x,y);

a.Name="a"+i;

a.Size = new System.Drawing.Size(100,20);

y=y+30;

this.grpFieldPanel.SendToBack();

this.grpFieldPanel.Controls.Add(a);

this.Controls.Add(a);

this.grpFieldPanel.ResumeLayout(false);

this.ResumeLayout(false);

}

}
 
Shayer,

I think that you should only be adding the new TextBox to either to the
grpFieldPanel variable (whatever control that might be), or the Controls
collection. A control can have at most only one parent and I think this
might be part of the problem.

Hope this helps.
 
Back
Top