Using Delegates to Interchange Information Between Siblings of a Parent Form

Introduction

Programmers often need to design MDI applications. In many cases, along with the MDI applications, you need to interchange information between child forms that are instantiated by the same parent. In such cases, delegates are a suitable way to exchange information. See how.

About the Program

In this simple program, you will have a parent form and two child forms. The child forms will be instantiated by the parent. The second child form will interact with the first child form via the parent form using delegates. That is all. The sample program simply shows how use delegates to commit this interaction. You can utilize the same tactics to develop other similar kinds of procedures.

The Details

This is the Parent Form. It is a container of the two child forms that you will create later.

This is the First Child that will appear when the “First Child” button of the parent form is clicked.

This is the Second Child that will appear when the “Second Child” button of the parent form is clicked.

When all three forms are there, the fun begins. Whatever the user writes in the text box of the second child (and then clicks Submit), that text is displayed in the label of the first child. This interaction between the two siblings is monitored and handled by the parent form. See how.

When the first child is instantiated, the parent form gets a pointer to a function of the form.

private void btnFirst_Click(object sender, EventArgs e)
{
   frmFirstChild FrmFirst = new frmFirstChild();
   FrmFirst.MdiParent = this;
   FrmFirst.Show();
   SayHello = FrmFirst.FuncDisplayMsg;
}

That is, the SayHello Delegate of the parent form keeps a pointer to a method of the first form. The declaration of the delegate is as follows:

private delegate void DelSayHello(string Msg);
private DelSayHello SayHello;

And the method that will be called is:

public void FuncDisplayMsg(string Msg)
{
   this.lblMsg.Text = Msg;
}

Simple. Now the catch is, the parent will call this method of the first child when a method of the second child passes a value to the parent. To do this, the parent has a method that will be called from the second child (via delegate). The method is:

private void InvokeFunc(string Msg)
{
   if (SayHello != null)
      SayHello.Invoke(Msg);
}

What happens when the second child is instantiated? The second child has a delegate that gets a pointer to the above-mentioned method. The code of the click event is as follows:

private void btnSecond_Click(object sender, EventArgs e)
{
   frmSecondChild FrmSecond = new frmSecondChild();
   FrmSecond.MdiParent = this;
   FrmSecond.InvokeDel = this.InvokeFunc;
   FrmSecond.Show();
}

And finally, when the user clicks on the Submit button of the second child, the text in the text box is passed to the parent form, which in turn passes the value to the first child, and the text is displayed.

public delegate void DelInvoke(string Msg);
public DelInvoke InvokeDel;

private void btnSubmit_Click(object sender, EventArgs e)
{
   if (this.InvokeDel != null)
      InvokeDel.Invoke(this.txtMsg.Text);
}

That’s it. Let me summarize everything. You are working in an MDI application that has two child forms. You want to pass a value from one child to the other. You use the parent as the medium of this data flow. Say, a simple message is to be passed from Second Child to First Child. The second child passes the data to the parent, and the parent passes the data to the first child. This is the simple summary, although the inner working might be difficult to understand for the timers. I tried to elaborate everything simply. Hope you enjoyed!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read