Use FormCollection to perform a iteration
Use FormCollection to hold all present and future open forms, these open forms can be accessed via Tag Name. Lest say the form to be accessed is Form1 and the textbox and labels tag names are "FirstName_text" and "First_Name_label".
// All open forms in your application will be placed in fcOpenForms.
FormCollection fcOpenForms = Application.OpenForms;
// Perform iteration for the form to be accessed by it's tag name.
foreach(Form fOpenForm in fcOpenForms.OfType<Form>().ToList()) {
if(fOpenForm.Tag != null && fOpenForm.Tag.Equals("Form1")) {
//Now iterate for the textbox tag name.
//
foreach(TextBox tFirstNameTextBox in fOpenForm.Controls.OfType<TextBox>().ToList()) {
if(tFirstNameTextBox.Tag != null && tFirstNameTextBox.Tag.Equals("FirstName_text")) {
//Now iterate for the label's tag names.
//
foreach(Label lFirstNameLabel in fOpenForm.Controls.OfType<Label>().ToList()) {
if(lFirstNameLabel.Tag != null && lFirstNameLabel.Tag.Equals("First_Name_label")) {
// You can now access these two controls "FirstName_text" and
// "First_Name_label" by calling it's variable name not the controls name.
//
tFirstNameTextBox.Text = "Your Text Here";
lFirstNameLabel.Text = "invalid first name";
// Any other control can be accessed using the same technique. You can either iterate
// for "Error_Label" here or up under where "First_Name_label" was accessed, it depends
// on where you see fit, to suite your needs at the time of accessing these controls in
//your application.
foreach(Label lErrorLabel in fOpenForms.Controls.OfType<Label>().ToList()) {
if(lErrorLabel .Tag != null && lErrorLabel .Tag.Equals("Error_Lable")) {
lErrorLabel.ForColor = Color.Pink;
}
}
}
}
}
}
}
}
If any control resides in a child container of the form being accessed, then that child container will have to be accessed respectively, before the controls can be accessed, for instance a groupbox e.g. foreach(GroupBox gGroupBox in fOpenForms) and then foreach(Control cChildControl in gGroupBox.Controls) etc. Some properties of a control being accessed in this manner will have to be casted, for example to Cast ((TextBox)tFirstNameTextBox).Text = "Your Text Here";
Hope this helps ...