且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过使用C#Windows应用程序单击按钮在运行时动态添加文本框

更新时间:2023-12-06 15:27:40

您的文本框都位于同一位置,并且都具有相同的名称.您可能会获得许多位于同一位置的文本框,因此它们彼此覆盖,或者因为名称相同,所以您的新文本框将替换旧的文本框.
Your text boxes are all in the same place, and all have the same name. You''re either getting many text boxes that are all in the same place, so they cover each other, or because the name is the same, your new one is replacing your old one.


;););););););)您应该在按下按钮后更改文本框的位置.所有位置都应不同.:cool :: cool :: cool :: cool:
像这样

;) ;) ;) ;) ;) ;) you should change your textboxs location after cliking button . all location should be diffrent.:cool::cool::cool::cool:
like this

string x=40;
private void button1_Click_1(object sender, EventArgs e)        {            
           txtRun = new TextBox();            
           txtRun.Name = "txtDynamic";            
           txtRun.Location = new System.Drawing.Point(x+20,18);
           x+=10;
           txtRun.Size = new System.Drawing.Size(200, 25);            
           this.Controls.Add(txtRun);        
}


嗨neetika2010,

只需在定位点"中设置"y"值即可.
Hi neetika2010,

Just set ''y'' value in Location Point.
//Assign this Value in Global
int x=100;
int y=25;

// In Your Button Event
private void button1_Click_1(object sender, EventArgs e)        {
           txtRun = new TextBox();
           txtRun.Name = "txtDynamic";
           txtRun.Location = new System.Drawing.Point(x,y);           
           txtRun.Size = new System.Drawing.Size(200,25);
           y=y+25;  
           this.Controls.Add(txtRun);
}



如果在文本框之间需要较大的空间,则只需在y中添加一个值即可.
例如:y = y + 35;

在使用UserControl的我的建议"中,您可以一个接一个地创建任何控件.

谢谢:)



If you need a large space in between a Textbox just add a value in y.
ex: y=y+35;

In my Suggestion using UserControl you can create any control one after another.

Thanks :)