0% found this document useful (0 votes)
12 views

Passing data between two Forms in WinForms

The document discusses methods for passing data between two Forms in WinForms, emphasizing the importance of maintaining a reference to the original Form to avoid creating multiple instances that do not share data. It outlines two main approaches: pushing data back to the original Form and pulling data from the second Form, with a preference for the latter due to its reusability and maintainability. The article provides code examples and highlights common pitfalls in handling data between Forms.

Uploaded by

Doctor Ammooor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Passing data between two Forms in WinForms

The document discusses methods for passing data between two Forms in WinForms, emphasizing the importance of maintaining a reference to the original Form to avoid creating multiple instances that do not share data. It outlines two main approaches: pushing data back to the original Form and pulling data from the second Form, with a preference for the latter due to its reusability and maintainability. The article provides code examples and highlights common pitfalls in handling data between Forms.

Uploaded by

Doctor Ammooor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Passing data between two Forms in WinForms

MENU

INSTANTIATION, WINFORMS, CODE STYLE, PROGRAMMING

Passing data between two Forms in WinForms


December 12, 2014 — 7 min read — 0 Comments

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

Yes, WinForms is an older technology, and no, there's nothing particularly sexy about it. But it works
and it's quick to spin up a new program. Many businesses still have legacy apps built with it, and its
drag-and-drop interface makes it easy to use in a classroom setting too.

True to its name, most things you do in WinForms revolve around Forms, and the creation of multiple
Forms for performing different functions. Functions like collecting data from the user, or displaying a
record from the database for the user to edit. After the user enters the requested data, or makes
changes to that record, and presses your OK button to close the current Form, their changes are lost
unless you do something with them first.

You might want to save the changes to a database at this point, but more often you'll just want to pass
the data back to the original Form that created the one that was just closed.

Note: All the following code in this post is also available on Github.

How do I pass data between two Forms?

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

I see this question asked a lot on Stack Overflow, and it's usually one of these slight variations:

How do I pass data between two forms?


How do I access data from Form1 (or Form2) in Form2 (or Form1)?
How do I get the contents of a <TextBox / List / etc> from another form?
How do I enter data into a form, and then close it and display it in my first form?

Let's defne our environment

Every program's different, but this particular issue has a few practical, common solutions. Let's mockup
a question first - this is pretty typical of the questions I see:

I have two Forms. The first Form has a button for opening the second Form.

The second Form collects input from the user in some TextBox controls. When they're done
and close it, I need to pass those values back to the first Form.

Here's is my code but it's not working as expected. How do I get what the user entered back
into the first Form?

public class Form1 : Form


{
public Form1() { }

private void btnGetUserInput_Click(object sender, EventArgs e)


{
Form2 form2 = new Form2();
form2.ShowDialog();
}
}

public class Form2 : Form


{
public Form2() { }

private void btnSaveInput_Click(object sender, EventArgs e)


{
Form1 form1 = new Form1();
form1.??? // How do I show my values on the first form?
form1.ShowDialog();
}
}

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

This is a very basic example. The first Form displays the data collected in the second Form, maybe in
a DataGridView or just some Labels. Or maybe the first Form takes some action on the data from the
second Form, like saving it to a file. Whatever.

Here's what the awesome app referenced in the question might look like:

So our mock environment is:

C#, WinForms
Two Forms, Form1 and Form2
Form1 starts with the program, and contains a DataGridView and a Button for opening Form2
Form2 has a couple TextBox controls for accepting user input, and a button to close the Form and
“Save” the input

Reference the original Form

A Form is just another class, and in order to change the properties of any class, you need to have a
reference to the particular instance you wish to modify.

Look at the code in the btnSaveInput_Click() event method above. If you're trying to create a new
instance of a Form in order to update it, you're making a fundamental mistake about how class
instantiation works. Multiple instances of a class do not automatically share data between them.

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

In other words, when you create a second instance of Form1 inside of Form2, it has nothing to do with
the original instance of Form1 that you started out with. This second instance of Form1 will go out of
scope and become inaccessible when the btnSaveInput_Click() method ends, and the values you
set in it will not carry over to the original Form1.

(You might be tempted to define public static fields that are easily accessible from everywhere, but try
to avoid it. The natural progression is then a static class to hold those static fields, and pretty soon you
have a blob often called the god class that's nigh impossible to debug. Been there, seen that.)

You have two practical choices for getting the data back into the first Form (Form1):

While you're still in Form2, push the data back to Form1.


Once you return to Form1, pull the data from Form2.

Pushing data back to Form1

First, I'll explain how you could do this, then I'll explain why you shouldn't.

In order to push data back to Form1, you need a reference to it. In general, the easiest way to do that
is to pass a reference in the constructor when you instantiate Form2:

public partial class Form2 : Form


{
public Form1 form1;

public Form2(Form1 form1)


{
InitializeComponent();
this.form1 = form1;
}

...

Now you can access public properties, methods, whatever on the first Form.

public partial class Form1 : Form


{
public Form1() { }

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

private void btnGetUserInput_Click(object sender, EventArgs e)


{
// Notice the 'using' statement. It helps ensure you clean up resources.
using (var form2 = new Form2(this))
{
form2.ShowDialog();
}
}

public void SetName(string name)


{
lblName.Text = name;
}

public int Age


{
set { lblAge.Text = value.ToString(); }
}
}

public partial class Form2 : Form


{
private Form1 form1;

public Form2(Form1 form1)


{
InitializeComponent();
this.form1 = form1;
}

private void btnClose_Click(object sender, EventArgs e)


{
form1.SetName(txtName.Text);

int age;
if (int.TryParse(txtAge.Text, out age))
form1.Age = age;

this.Close();
}
}

Issue #1: Reusability

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

The first issue I have with this method is reusability. Imagine that next week, you want to use Form2
from another Form, say Form3. You want to collect the same data but present it in a different manner.
Now your Form isn’t as reusable, because it's not so clear who will be calling the Form.

Issue #2: Too much knowledge

In general, a thing being called should know little to nothing about the thing calling it.

There is no reason for Form2 to know about the other Forms, user controls, class libraries, etc that
could potentially use it. You should not have a bunch of if/else statements to handle this kind of logic.
Imagine the following, where we have two Forms (an employee Form and a student Form), and an
additional class library with no UI of its own, all using the Details Form to get name and age.

public partial class StudentForm : Form


{
private void btnGetStudentData_Click(object sender, EventArgs e)
{
using (var detailForm = new DetailForm(this))
{
detailForm.ShowDialog();
}
}

public void SetStudentName(string name)


{
lblStudentName.Text = name;
}
}

public partial class EmployeeForm : Form


{
private void btnGetEmployeeInput_Click(object sender, EventArgs e)
{
using (var detailForm = new DetailForm(this))
{
detailForm.ShowDialog();
}
}

public string EmployeeName { get; set; }


}

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

public class SomeClass


{
public void SomeMethod()
{
using (var detailForm = new DetailForm(this))
{
detailForm.ShowDialog();
}
}

public string Name { get; set; }


}

public partial class DetailForm : Form


{
private StudentForm studentForm;
private EmployeeForm employeeForm;
private SomeClass someClass;

public DetailForm(StudentForm form)


{
InitializeComponent();
studentForm = form;
}

public DetailForm(EmployeeForm form)


{
InitializeComponent();
employeeForm = form;
}

public DetailForm(SomeClass someClass)


{
InitializeComponent();
this.someClass = someClass;
}

private void btnClose_Click(object sender, EventArgs e)


{
if (studentForm != null)
studentForm.SetStudentName(txtName.Text);
else if (employeeForm != null)
employeeForm.EmployeeName = txtName.Text;
else if (someClass != null)
someClass.Name = txtName.Text;

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

this.Close();
}
}

The DetailForm Form knows way too much about what's potentially calling it, and has to plan for
every possible caller - things get ugly quick. Also, making changes to one of the callers requires
changing the shared Form as well.

Let's look at another way...

Pulling data from Form2

A better option is to make the data available from the second Form, then let the individual callers
retrieve the data (or not) as they choose. The easiest way to do this is to create public "getter"
methods on the second Form, which I'll show below.

(If you're unfamiliar with getter/setter properties, check out this thread - it's 10 years old and for Java,
but the reasoning is the same for C# and remains relevant even today.)

Here's a copy/paste of the previous code block, modified so that the shared Form knows nothing about
its callers.

public partial class StudentForm : Form


{
private void btnGetStudentData_Click(object sender, EventArgs e)
{
using (var detailForm = new DetailForm())
{
detailForm.ShowDialog();

lblStudentName.Text = detailForm.Name;
}
}
}

public partial class EmployeeForm : Form


{
private void btnGetEmployeeInput_Click(object sender, EventArgs e)
{
using (var detailForm = new DetailForm())
{

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

detailForm.ShowDialog();

EmployeeName = detailForm.Name;
}
}
}

public class SomeClass


{
public void SomeMethod()
{
using (var detailForm = new DetailForm())
{
detailForm.ShowDialog();

Name = detailForm.Name;
Age = detailForm.Age;
}
}

public string Name { get; private set; }


public string Age { get; private set; }
}

public partial class DetailForm : Form


{
public DetailForm()
{
InitializeComponent();
}

public string Name


{
get { return txtName.Text; }
}

public int Age


{
get
{
int result;
Int32.TryParse(txtAge.Text, out result);
return result;
}
}

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

// Even this could be removed, if you set this button as the Accept
// button on the Form, or set a DialogResult value on the button
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}

The second Form is left cleaner. It requires no special knowledge of its callers, and has greater
reusability and maintainability. The callers can grab as much or as little data as they need, or do
nothing at all.

You can use this method to retrieve whatever data you need from the child Form (Form2), such as a
List or TextBox values.

More Reading

I hope this helped clarify a few things. If it still seems unclear, or you see a possible error somewhere,
please leave a comment below and we’ll figure it out!

This is a really common question for beginners. You can do a search for "passing between forms
winforms" and you'll get quite a few hits. Here are a few good ones. I haven't read them all through
end-to-end, but they're thorough and will give you something to think about, at least.

Passing Data Between 3 Separate WinForms


Passing Values Between C# Winforms Working Intermittently
Passing string values from one form to another
Send values from one form to another form
Passing variable between winforms
Passing Values Between Forms in .NET
Passing Data Between Forms

Note: The code in this post is also available on Github.

Photo (used in header) by Hal Gatewood / Unsplash

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

Share:

WRITTEN BY

Grant Winney

I write when I've got something to share - a personal project, a solution to a


difficult problem, or just an idea. We learn by doing and sharing. We've all
got something to contribute.

TWITTER

NEXT

Using Annotations to Assis ReSharper in Analyzing Your Code


on December 22, 2014

PREVIOUS

Using a TextBox and CollectionViewSource to Filter a LisView in WPF


on December 07, 2014

I occasionally include affiliate links for products I own and have found to be of good quality, or after checking reviews and
feeling confident they are good products. These links don't increase your cost at all, but using them helps pay for this blog and
the time I put into it. Thanks!

Comments

Sorry, the browser you are using is not currently supported. Disqus actively supports the following
browsers:

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

Firefox
Chrome
Internet Explorer 11+
Safari

Nick_F • 1 month ago

Thanks, the article was very useful.

Grant Winney • 1 month ago

Glad to hear it, thanks Nick.

Ej Malibong • 5 months ago

Hope you can provide code for setting combobox's selectedvalue or selecteditem using vb.net. Anyway,
you provide great help. Thanks.

WELCOME!

I write about whatever happens to be on my mind when I sit down. I'm a father of 5, developer by profession, and aspiring
woodworking hobbyist. I enjoy the Pi and other programming projects, as long as there's a purpose. A few more things about
me...

GET WEEKLY UPDATES

If you like what you read today, and you'd like more of the same, enter your email below to receive weekly updates.

email address

Subscribe

LATEST POSTS

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]


Passing data between two Forms in WinForms

Hosting a GitHub wiki on Ubuntu (and keeping it in sync)


October 29, 2018

The 5 Stages of Debugging Grief


October 19, 2018

Setting up the aquarium - Month 1


October 14, 2018

Querying Amazon's product details with the Amazon Product Advertising API
October 12, 2018

Trouble downloading the Windows 10 ISO - Message Code 715-123130


October 08, 2018

Don't be too quick to blame... it's rarely that simple


September 22, 2018

Prepping for a first time aquarium ("The Plan")


September 16, 2018

A few thoughts on date/time handling in Erlang


September 03, 2018

How to select an earlier .NET version on Visual Studio for Mac (tl;dr: you can't)
August 20, 2018

Two weeks of mentoring Exercism


August 10, 2018

BACK TO TOP
© 2018 Grant Winney (via Ghost and Wildbird) · Affiliate Links · Cross-Posting · License

file:///H/downloads2/Passing%20data%20between%20two%20Forms%20in%20WinForms.htm[PM ۰۲:٥٦:۱۷ ۱۸/۱۲/۱۸]

You might also like