我想知道如何使用 C# 获取表单中的所有文本框名称?
这是我生成动态文本框的代码:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 5; i++)
{
TextBox txtbox = new TextBox();
txtbox.Name = "txtbox" + i;
flowLayoutPanel1.Controls.Add(txtbox);
Label lbl = new Label();
lbl.Name = "lbl" + i;
lbl.Text = lbl.Name;
flowLayoutPanel2.Controls.Add(lbl);
}
}
private void button1_Click(object sender, EventArgs e)
{
string[] textBoxNamesArray = this.Controls.OfType<TextBox>()
.Select(r => r.Name)
.ToArray();
var textboxes = string.Join(",", textBoxNamesArray);
MessageBox.Show(textboxes);
}