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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Stories about devastating cyberattacks are plaguing the news. Why? The DNS protocol is easy to exploit. See your network the way hackers do—as an easy target. Learn how you can effectively secure your DNS infrastructure today.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds