Code:
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public class FormHelpers
{
public static Form GetForm(string formName)
{
if(formName != null && formName.Length > 0) {
foreach(Form form in Application.OpenForms) {
if(form.Name == formName) return form;
}
}
return null;
}
public static Control GetControl(Form form, string controlName)
{
if(form != null && controlName != null && controlName.Length > 0) {
foreach(Control control in form.Controls) {
if(control.Name == controlName) return control;
}
}
return null;
}
public static Control GetControl(string formName, string controlName)
{
return GetControl(GetForm(formName), controlName);
}
public static void Test()
{
TextBox tb = GetControl("Form1", "textBox1") as TextBox;
if(tb != null) MessageBox.Show(tb.Text);
}
}
}