Simple Way to Establish Interaction Between Two Froms Of An Application

The simple logic behind this approach of establishing an interaction between forms in a single Windows Forms application is to make the view and accessibility of one form to another form and its child controls too.

Introduction

After some play with Microsoft Visual Studio's Windows Forms I found very simple logic to establish communication between two forms within a Windows Forms application. Of course we have several forms of logic to establish communication between to forms to make them more interactive. But this is the one that may be the very simplest way to provide an interaction between the forms.

Concept behind the approach

The simple logic behind this approach of establishing an interaction between forms in a single Windows Forms application is to make the view and accessibility of one form to another form and its child controls too.

Also when implementing this logic at the code level, there will a query of how to access a Windows Forms form from another Windows Forms form? Yes, this is possible in a simple way by passing the instance to the next form though a constructor arguments or another way. But by passing the instance through the constructor argument is the optimal one.

C# Code behind the approach

  1. Create a form1 with a publicly declared TextBox (here the text box is used to show the interaction).
  2. Create another form2 of which has “form” as an argument parameter.
  3. Create form2 from form1 (by passing form1 as a parameter) and stored in form 1 to access the from 2.
  4. Store the form1 from the constructor of the form2.
  5. Now both forms are viewable and accessible to each other.

Form 1 Code snippet

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace InteractionForms

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        public System.Windows.Forms.TextBox textBox1; // Text Box

        Form2 connectedForm; // Varaible to store other form

        protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);

            connectedForm = new Form2(this); // Create a new instance of a from to open, by passing this as argument instance

            connectedForm.Show(); // Show the form

            this.textBox1.TextChanged += textBox1_TextChanged; // Hook the text changed event

        }

 

        void textBox1_TextChanged(object sender, EventArgs e)

        {

            connectedForm.textBox1.Text = this.textBox1.Text; // assign the current Text Box text to other form's Text Box text

        }

    }

} 

Form 2 Code snippet

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace InteractionForms

{

    public partial class Form2 : Form

    {

        public System.Windows.Forms.TextBox textBox1; // Text Box

        Form1 connectedForm; // varaiable to store the another form instance

        public Form2(Form form) // Getting the form throgh constructor argument

        {

            InitializeComponent();

            connectedForm = form as Form1;

            this.textBox1.TextChanged += textBox1_TextChanged; // hook the text changed event

        }

 

        void textBox1_TextChanged(object sender, EventArgs e)

        {

            connectedForm.textBox1.Text = this.textBox1.Text; // assign the current Text Box text to other form's Text Box text

        }

    }

}

 
interaction Flow 

Output

Output

Here the form 1 TextBox's text changes also make changes to form 2 TextBox's text and vice versa because of the very simple logic provided above behind this approach of establishing an interaction between forms in a single Windows Forms application is to make the view and accessibility of one form to another form and its child controls too.

Also while about to implement this logic throughat the code level, there will a query of how to access a Windows Forms form from another Windows Forms form? Yes, this is possible by a simple way by passing the instance to the next form though a constructor arguments or some otner way. But by passing the instant through the constructor argument is the optimal one.