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

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

  • Not all enterprise applications are created equal. Sophisticated applications need developer support but other more basic apps do not. With the right tools, everyone is a potential app developer with ideas and a perspective to share. Trends such as low-code development and model driven development are fundamentally changing how and who creates applications. Is your organization ready? Read this report and learn: The seven personas of enterprise app delivery How application ownership is spreading to the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds