Loop through components in a form?

  • Thread starter Thread starter Dante
  • Start date Start date
D

Dante

Does anyone know of a way to loop through all components of a certain type
in a form. Not all of the components are within the same container. They
actually are on separate tab pages.

Thanks.
 
Dante said:
Does anyone know of a way to loop through all components of a certain type
in a form. Not all of the components are within the same container. They
actually are on separate tab pages.

Use the Controls property, and recurse if necessary.
 
Check out this working example :

string[] myFont = new string[3];
Form f = new x()
if (f != null)
{
foreach (Control ctr in f.Controls)
{
if (ctr.GetType() == typeof(TextBox))
{
TextBox t = (TextBox) ctr;

if(t.Name.Equals("textBox1"))
{
myFont[0] = t.Text.Trim();
}
if(t.Name.Equals("Printerx"))
{
myFont[1] = t.Text.Trim();
}

if(t.Name.Equals("Printery"))
{
myFont[2] = t.Text.Trim();
}
}

}
 
Check out this working example :

string[] myFont = new string[3];
Form f = new x()
if (f != null)
{
foreach (Control ctr in f.Controls)
{
if (ctr.GetType() == typeof(TextBox))
{
TextBox t = (TextBox) ctr;

if(t.Name.Equals("textBox1"))
{
myFont[0] = t.Text.Trim();
}
if(t.Name.Equals("Printerx"))
{
myFont[1] = t.Text.Trim();
}

if(t.Name.Equals("Printery"))
{
myFont[2] = t.Text.Trim();
}
}

}
 
Back
Top