Adorn the MainForm with an interface, for example:
public partial class MainForm : Form, ISomeInterface
{
public MainForm()
{
InitializeComponent();
}
public void SendText(object sender, string text)
{
}
}
Then, to allow the
UserControl
s to call that code they need to be aware of the
MainForm
instance, typically this can be done by exposing a property on the
UserControl
:
public partial class SomeUserControl : UserControl
{
public SomeUserControl()
{
InitializeComponent();
}
public ISomeInterface SomeInstance { get; set; }
private void DoSomething()
{
if (SomeInstance != null)
SomeInstance.SendText(this, "Hello, world!");
}
}
In order to hook that up, you can (for example) iterate through the controls of
MainForm
in it's constructor:
public MainForm()
{
InitializeComponent();
foreach (Control control in Controls)
{
if (control is SomeUserControl)
((SomeUserControl)control).SomeInstance = this;
}
}