【发布时间】:2014-06-01 07:12:14
【问题描述】:
我使用下面的代码通过最初给出文本框的总数来动态创建文本框。在我向这些文本框输入值后,我如何从中获取值。就像我将计数设为 3 一样,将创建 3 个文本框。现在,我在每个文本框中输入数据。我如何读取我输入到这些 texbox 中的值。
int a = 1;
public Form1()
{
InitializeComponent();
}
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
txt.Top = a * 25;
txt.Left = 100;
txt.Name = "txt" + this.a.ToString();
txt.Text = "TextBox " + this.a.ToString();
a = a + 1;
return txt;
}
private void button1_Click(object sender, EventArgs e)
{
int i;
int count = Int16.Parse(counttxt.Text.ToString());
for (i = 1; i <= count; i++)
{
AddNewTextBox();
}
}
【问题讨论】:
-
循环遍历
Controls集合并检查每个类型是否为TextBox。