【发布时间】:2015-02-02 11:17:50
【问题描述】:
我现在有点迷茫,我创建了一些代码来创建 4 个文本框并在运行时将它们添加到表格布局中(下面的代码),但我正在努力获取文本从中,我尝试像 string s = TxtBox1.Text.ToString(); 一样从中获取值,但它只是获得一个空引用,然后我尝试了 txt.Text.ToString();,这只是从创建的最后一个文本框中获取文本。
private void button2_Click(object sender, EventArgs e)
{
int counter;
for (counter = 1; counter <= 4; counter++)
{
// Output counter every fifth iteration
if (counter % 1 == 0)
{
AddNewTextBox();
}
}
}
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
}
我已经到处寻找这个问题的答案,但如果有人有任何想法,我将不胜感激。
谢谢
【问题讨论】:
标签: c# winforms dynamic textbox