This reply is meant to complement the excellent advice given you by Dave K. Be sure and up-vote his solution.
Consider two scenarios:
1) notification: an event on one Form notifies another Form.
2) notification
and transfer of information or data: one Form requests information from another Form; or, one Form sends information to another Form.
The technique I think achieves the maximum de-coupling (isolation, separation of concerns) between Forms is to define a publicly exposed Func or Action (both are convenient ways to define a Delegate/EventHandler).
Do study Action [
^] and Func [
^] now. For a deeper overview: [
^]
When the instance of the Form with that Func or Action invokes it (runs it), it will broadcast to all of its subscribers. The Action can transfer whatever, but its invocation returns no result. The Func returns a result tom the calling code.
Assume Form1 here is the Main Form of a WinForm app; Form 2 is a Form that Form1 will create and use.
1) Notification
private Form2 form2;
private void Form1_Load(object sender, System.EventArgs e)
{
form2 = new Form2();
form2.SendData +=
}
private void Form2NotificationReceiver(DateTime dt, string msg)
{
}
public Action<DateTime, string> SendData { set; get;}
private void NotifyButton_Click(object sender, System.EventArgs e)
{
string msg = "NotifyButton Clicked";
if (SendData != null)
{
SendData(DateTime.UtcNow, msg );
}
}
Summary: an Action (delegate) is inserted into Form2 by Form1' at run-time, a Button click in Form2 executes the Action which calls/invokes a handler in Form1.
Form2 "knows nothing" about Form1, it can't access Formq in any way. Form1's access to Form2 is a one-way transmission link.
Will round this out with a Func example tomorrow ... energy allowing.