Pass values between 2 MDI Child Forms using C# and VB.NET

Recently, I came across a situation where I need to pass value from one Child Form to another Child Form inside an MDI Parent. The scenario was like this. I have an MDI, whose MenuStrip pops out 2 Child Forms together. One child-form lists the Id and Name of all the Employees coming under certain criteria. On double clicking that particular employee, I need to display the value on the second child-form. In short, I need to pass the Id of the employee to the second child-form so that relevant details can be loaded. I’ll simulate the same scenario using a simple example.

Imagine I want to send a value from one of my Child form to display that in my second Child Form. The sample UI looks like this:


[Send Value] –> ChildForm1.cs

   1: /// <summary>
   2: /// Send Value
   3: /// </summary>
   4: /// <param name="sender"></param>
   5: /// <param name="e"></param>
   6: private void button1_Click(object sender, EventArgs e)
   7: {
   8:     ChildForm2 formChild2 = (ChildForm2)this.MdiParent.MdiChildren[1];
   9:     formChild2.ReceiveValue(textBox1.Text);
  10: }

[Receive Value] –> ChildForm2.cs

   1: // Receive Value
   2: public void ReceiveValue(string value)
   3:         {
   4:             textBox1.Text = value;
   5:             this.Activate();
   6:             this.Refresh();
   7:         }



[Send Value] –> ChildForm1.vb

   1: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   3:         Dim formChild2 As ChildForm2 = DirectCast(Me.MdiParent.MdiChildren(1), ChildForm2)
   4:         formChild2.ReceiveValue(TextBox1.Text)
   6:     End Sub


[Receive Value] –> ChildForm2.vb

   1: Public Sub ReceiveValue(ByVal value As String)
   2:        TextBox1.Text = value
   3:        Me.Activate()
   4:        Me.Refresh()
   5:    End Sub

Hope this helps.