Using Delegates to Interchange Information Between Siblings of a Parent Form


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;
   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)

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;

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)

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!

About the Author

Mehedi Shams

Graduated in CSE from Khulna University, Bangladesh.
Software Engineer in professional life.



  • purchase Diazepam online without a prescription

    Posted by CicGamidioxia on 11/15/2012 05:45am

    Valium Without Prescription Cheap Valium Online No Rx - Buy Valium Online Medication

  • Vondool go ahead

    Posted by asadmoin on 12/04/2007 01:20am

    Thank you for sharing such a nice and helpful idea.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date