I want to know how can I get all the textbox names in a form using C#?
Here is my code in generating dynamically textboxes:
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);
}
You can use LINQ to get all the names of the controls of type
TextBoxfrom the current form. The following query will return you an array of strings containing all the names.Remember to include
using System.Linq;