Passing Data Between Forms Without Events and Delegates


Introduction

This article describes a simplified approach to allowing communication between forms without the use of events and delegates. The approach demonstrated is adequate if the application uses a single instance of a form and allows the creation of a single instance of a dialog used to pass data to the main calling form. If you need to broadcast the data generated by a dialog to multiple form listeners, you will still need to work with events and delegates; this approach is only valid if there is a one on one relationship between the two interactive forms.



Figure 1.  Data entered in the Right Form immediately appears in the Left Form
 

Getting Started.

There is a single Win Forms application included with this download. You may open the project and examine the files and code if you wish; however, the code is quite simple and will be described in the following sections of this document. All examples were written using Visual Studio 2005 and are in C#; the same code could also be used in earlier versions of Visual Studio.

In general the application contains only two forms. Form 1 opens with the application and it contains five label controls which act as targets for data entered from Form 2. Form 1 is titled, "Originator" and Form 2 is labeled, "Data Entry".  

Code:  Form 1.

Form 1 contains five labels and a button. The five labels carry default text whenever an instance for Form 1 initially created. The labels are set to display "Name", "Street", "City", "State", and "Zip Code". There is also a single button contained on the form. Whenever this button is clicked, a new instance of Form 2 is created. Form 2 has an overloaded constructor and it accepts an object of type Form 1 as an argument. By passing the current Form 1 to instances of Form 2 through the constructor, it is possible to communication directly between the forms without adding events and delegates.

The entire body of code contained in Form 1 is as follows:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace LimitedDataXfer

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void btnOpenForm_Click(object sender, EventArgs e)

        {

            Form2 f = new Form2(this);

            f.Show();

        }

    }

}

Looking over the code you will not that nothing but the default imports are included in the definition of the form class. The class definition is also in the default configuration. The only code added to the class is that used to handle the single buttons click event. In that click event handler, a new instance of Form 2 is created and it is pass "this" as an argument. After the current Form 1 is passed to the form 2 constructor as an argument, the Form 2 instance is displayed to the user.

By passing the current Form 1 object to Form 2 in this manner, Form 2 may directly access any property in Form 1 instance to include each of the labels that will be populated through Form 2. This will allow communication between the two forms without the definition an delegates or events used to handle that communication. 

Code:  Form 2.

The code behind Form 2 is also very simple. This form also contains only the default imports and the class declaration is also per the default configuration. The form does contain an overloaded constructor which is configured to accept an object of type Form 1 as an argument. If a Form 1 object is passed to the constructor, a local object of type Form 1 will be set to point to this passed in form.

The entire body of code contained in this class is as follows:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace LimitedDataXfer

{

    public partial class Form2 : Form

    {

        Form1 f;

 

        public Form2()

        {

            InitializeComponent();

        }

 

        public Form2(Form1 fr1)

        {

            InitializeComponent();

            f = new Form1();

            f = fr1;

        }

 

        private void Form2_Load(object sender, EventArgs e)

        {

          

        }

 

        private void textBox1_TextChanged(object sender, EventArgs e)

        {

            f.lblName.Text = textBox1.Text;

        }

 

        private void textBox2_TextChanged(object sender, EventArgs e)

        {

            f.lblStreet.Text = textBox2.Text;

        }

 

        private void textBox3_TextChanged(object sender, EventArgs e)

        {

            f.lblCity.Text = textBox3.Text;

        }

 

        private void textBox4_TextChanged(object sender, EventArgs e)

        {

            f.lblState.Text = textBox4.Text;

        }

 

        private void textBox5_TextChanged(object sender, EventArgs e)

        {

            f.lblZip.Text = textBox5.Text;

        }

    }

}

In the code above, you will note that the overloaded constructor is configured to accept the Form 1 object as an argument and you can see that the local Form 1 object is set to this passed in Form 1 object. The remainder of the code is merely used to handle the text changed event for each of the text boxes contained in the Form 2 object. Whenever the text in any of the text boxes is changed, the current value of that text box is immediately passed to the appropriate label control in the Form 1 object.

When the application is run and the Form 2 object is created, typing into any of the text boxes will immediately update the Form 1 object's applicable label control.   

Summary.

This example demonstrates a simplified approach to transmitting data between forms without any reliance upon delegates and events. Since the approach shown requires passing the calling form as an argument to the new form which in turn will operate on the calling form, the approach is limited to instances where that relationship may be enforced and where communication between the two forms is adequate. If you need to broadcast information to multiple forms, you will need to rely on events and delegates to accomplish the task.

Up Next
    Ebook Download
    View all
    Learn
    View all