我想知道如何使用C#獲取表單中所有文本框的名稱?使用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);
}
謝謝,我會試試這個.. :) – GrayFullBuster
嘿,爲什麼它在消息框上是空白的? :) – GrayFullBuster
更新我的代碼:) – GrayFullBuster