Communication Between Two Forms

Overview

The aim of the program is to send a message between different forms. If you have a background in visual basic you will find it quite difficult because even if you make the objects public, when you try to send messages from one form to another it will have no effect.

Description

We have to distinguish between two different situations:

  1. The form isn't open yet;
  2. The form is already open.

The first case is easier than the second one, because you only need to make the object in the second form public (better if you make a public function) and use it before using the show() method.

In the second case you need to use delegation.

Lets start with the first problem:

In the second form (called frmNext.cs) I made a public function to write something in the textBox1:

  1. public void writeMessage(string str)  
  2. {  
  3.     this.textBox1.Text = str;  
  4. }


in the main form (called frmMain.cs) when I click the button:
  1. private void button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.     me.writeMessage("hello from main");  
  4.     me.Show();  
  5. }


Now lets look at the second problem - sending a message to the first form. First we need to declare the delegate and the event in the frmNext.cs:
  1. public delegate void Message(string str);  
  2. public event Message OnMessage;
we need to create a function to send the message in the frmMain.cs:
  1. private void txtMessage(string str)  
  2. {  
  3.     this.textBox1.Text = str;  
  4. }
Then we need to set the reference of the event in the frmNext.cs:
  1. private void InitializeComponent()  
  2. {  
  3.     this.button1 = new System.Windows.Forms.Button();  
  4.     this.textBox1 = new System.Windows.Forms.TextBox();  
  5.     this.SuspendLayout();  
  6.     //  
  7.     // button1  
  8.     //  
  9.     this.button1.Location = new System.Drawing.Point(112, 144);  
  10.     this.button1.Name = "button1";  
  11.     this.button1.TabIndex = 0;  
  12.     this.button1.Text = "button1";  
  13.     this.button1.Click += new System.EventHandler(this.button1_Click);  
  14.     //  
  15.     // textBox1  
  16.     //  
  17.     this.textBox1.Location = new System.Drawing.Point(112, 96);  
  18.     this.textBox1.Name = "textBox1";  
  19.     this.textBox1.TabIndex = 1;  
  20.     this.textBox1.Text = "textBox1";  
  21.     //  
  22.     // frmMain  
  23.     //  
  24.     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);  
  25.     this.ClientSize = new System.Drawing.Size(292, 266);  
  26.     this.Controls.AddRange(new System.Windows.Forms.Control[] this.textBox1, this.button1});  
  27.     {  
  28.         this.Name = "frmMain";  
  29.         this.Text = "Form1";  
  30.         this.ResumeLayout(false);  
  31.         me.OnMessage += new frmNext.Message(this.txtMessage);  
  32.     }  

and finally we need to use the event in the frmNext.cs int button1_click event:
  1. private void button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.     this.OnMessage("Hello");  
  4. }



dont forget you need to hide the frmNext form when youre closing the form otherwise, if you close the form and then try to call it again youll have an error.

So put this code in the form_closing event:

  1. private void frmNext_Closing(object sender,System.ComponentModel.CancelEventArgs e)  
  2. {  
  3.     e.Cancel = true;  
  4.     this.Hide();  
  5. }