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!



About the Author

Mehedi Shams

Graduated in CSE from Khulna University, Bangladesh.
Software Engineer in professional life.
mehedishams@gmail.com

Downloads

Comments

  • 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

    Reply
  • Vondool go ahead

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

    Thank you for sharing such a nice and helpful idea.

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

Top White Papers and Webcasts

  • By providing complete access control with granular permissions, deployment flexibility, mapped drive support, and ability to transfer large files, Egnyte provides a more robust, secure and an affordable file sharing solution for the business than Box

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds