C# Programming > Windows Forms

C# Passing Data Between Forms

 

Values between Windows Forms

How to pass data between Forms in C# is not very obvious, but it is also not very difficult. The trick is to remember Window Form objects are classes and thus have all the behaviors of a .NET class.

Passing data between Windows Forms is important when working with dialog boxes for example. Or simply when a system relies on input from more than one window.

Using Parameters

Since C# Forms are still .NET classes, the easiest way to pass data to a new Form is to use parameters in the constructor. Most of the time a Windows Form constructor looks something like this:

public MyForm()
{ InitializeComponent(); }

So to pass values to MyForm we can add an overload of MyForm() that takes some values as parameters, for example:

private string _Messge;
public MyForm(string Message) : this()
{ this._Message = Message; }

Two things to note about the example. First of all, passing the values through parameters is not enough. Once the child Form has been initialized with some values, the values need to be stored in field so the rest of the Form can access them. The second thing is overloads of the constructor should call the original constructor (done with this()).

Using Properties

Parameters are great for passing values between Forms. But you will quickly run into the scenario where we want to pass data to another Form, and then get some values back.

A possible solution is to pass as a parameter the reference of the parent Form to the new window. Then the new window can use the reference to update field variables inside the parent. However this is a terrible way to write programs. This is what software engineers would refer to as "high coupling", which is the opposite of what good software should have.

A much better way is to use Properties. In case you don't know, an example of class properties is Width, or Height of a Windows Form. What we are going to do is add our very own properties, which will allows us to pass data to another Form and then get it back.

To use the example above, let's make a Message property:

private string _Messge;

public MyForm()
{
     InitializeComponent();
}

public string Message
{
     get { return _Message; }
     set { _Message = value; }
}

Remember that _Message is a field variable.

What we can do now is create a new instance of MyForm without any parameters. Instead, we assign the value to the Message property of the MyForm instance before calling the Form. After the Form closes, we can get the value back with the same property. The only thing MyForm has to do is update _Message if the value needs to be changed. So the C# code might look something like:

MyForm child = new MyForm();
child.Message = "hello!";
child.ShowDialog();
string newMessage = child.Message;

Using Both

Of course using parameters or using properties to pass data between Windows Forms in .NET each have their own advantages. They also don't have to be exclusive, using a combination of both is perfectly acceptable as long as it is done intelligently.

Back to C# Article List