通过使用C#Windows应用程序单击按钮在运行时动态添加文本框 [英] Adding Text box dynamically at run time by clicking on button using C# windows aplication

查看:55
本文介绍了通过使用C#Windows应用程序单击按钮在运行时动态添加文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试获取TextBox.每当我单击按钮时,我都会得到一个TextBox.这是我的代码.但是我只得到一个TextBox.

Hi,
I am trying to get TextBox. Whenever I click on the button I should get a TextBox. Here is my code. But I am getting only one TextBox.

private void button1_Click_1(object sender, EventArgs e)
{
    txtRun = new TextBox();
    txtRun.Name = "txtDynamic";
    txtRun.Location = new System.Drawing.Point(20, 18);
    txtRun.Size = new System.Drawing.Size(200, 25);
    // Add the textbox control to the form''s control collection
    this.Controls.Add(txtRun);
}



我们将不胜感激.



Any help would be appreciated.

推荐答案

您的文本框都位于同一位置,并且都具有相同的名称.您可能会获得许多位于同一位置的文本框,因此它们彼此覆盖,或者因为名称相同,所以您的新文本框将替换旧的文本框.
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 :)


这篇关于通过使用C#Windows应用程序单击按钮在运行时动态添加文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆