Create base class, and implement common logic there. Then inherit controls from this base class. For accessing controls from base class level make abstract get properties on base class level for every single control you'll need. Implement those properties in child control classes.
public abstract class BaseControl : UserControl
{
public abstract TextBox FirstName { get; }
public void SomeLogicExample()
{
FirstName.Text = "Something";
}
}
public class ControlA : BaseControl
{
public override TextBox FirstName
{
// txtFirstNameA is ID of TextBox, so it is defined in ControlA.designer.cs
get { return txtFirstNameA; }
}
}
public class ControlB : BaseControl
{
public override TextBox FirstName
{
// txtFirstNameB is ID of TextBox, so it is defined in ControlB.designer.cs
get { return txtFirstNameB; }
}
}
Alternatively and a lot less elegant is locating controls at runtime; because you pay for searching the whole control tree and you have to handle control not found scenarios:
public abstract class BaseControl : UserControl
{
public T GetControlByType<T>(Func<T, bool> predicate = null) where T : Control
{
var stack = new Stack<Control>(new Control[] { this });
while (stack.Count > 0)
{
var control = stack.Pop();
T match = control as T;
if (match != null)
{
if (predicate == null || predicate(match))
{
return match;
}
}
foreach (Control childControl in control.Controls)
{
stack.Push(childControl);
}
}
return default(T);
}
public TextBox FirstName
{
get { return GetControlByType<TextBox>(t => t.ID == "txtFirstName"); }
}
public void SomeLogicExample()
{
FirstName.Text = "Something";
}
}