这是我的代码.但是我所有文本框的值都是空的.
This is my code. But all my textboxes's value is just null.
public void createTxtTeamNames()
{
TextBox[] txtTeamNames = new TextBox[teams];
int i = 0;
foreach (TextBox txt in txtTeamNames)
{
string name = "TeamNumber" + i.ToString();
txt.Name = name;
txt.Text = name;
txt.Location = new Point(172, 32 + (i * 28));
txt.Visible = true;
i++;
}
}
感谢您的帮助.
数组创建调用只是将元素初始化为null
.您需要单独创建它们.
The array creation call just initializes the elements to null
. You need to individually create them.
TextBox[] txtTeamNames = new TextBox[teams];
for (int i = 0; i < txtTeamNames.Length; i++) {
var txt = new TextBox();
txtTeamNames[i] = txt;
txt.Name = name;
txt.Text = name;
txt.Location = new Point(172, 32 + (i * 28));
txt.Visible = true;
}
注意:正如一些人所指出的,为了使这段代码有意义,您需要将每个 TextBox
添加到父 Control
.例如 this.Controls.Add(txt)
.
Note: As several people have pointed out in order for this code to be meaningful you will need to add each TextBox
to a parent Control
. eg this.Controls.Add(txt)
.
这篇关于动态创建多个文本框 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!