Click here to Skip to main content
12,299,012 members (52,360 online)
Click here to Skip to main content
Add your own
alternative version


103 bookmarked

Passing Data Between Forms

, 16 May 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
This article is aimed at providing some simple methods for passing data between forms in Windows applications

Sample Image - PassData.jpg

Passing Data Between Forms


Some of you would have faced a scenario where you wanted to pass data from one form to another in WinForms. Honestly, I too had a similar problem (that’s why I am writing this article!).

There are so many methods (How many? I don't know) to pass data between forms in Windows application. In this article, let me take four important (easiest) ways of accomplishing this:

  1. Using constructor
  2. Using objects
  3. Using properties
  4. Using delegates

Let us see all the above methods in detail in the following sections.

For data to be passed between forms using any of the above methods, we need two forms and some controls. Let us start by following the steps given below.

Step 1

Create a new project and select Windows application. This will create a default form as “Form1”. We can use this form for sending data.

Step 2

Add a textbox and a button to the form.

Step 3

Add another Windows Form for receiving the data and to display it. Right click the project and select Add->Add Windows Form. Enter a name or use the default name “Form2.cs” and click ok button.

Step 4

Add a label to the second form to display the text from form1.

The Constructor Approach

This could be the easiest method of all. A method is invoked whenever you instantiate an object. This method is called a constructor. Code a constructor for form2 class with one string parameter. In the constructor, assign the text to the label’s text property. Instantiate form2 class in form1’s button click event handler using the constructor with one string parameter and pass the textbox’s text to the constructor.

Follow the steps given below:

Step 1

Code a constructor for form2 class as below:

public Form2(string strTextBox)

Step 2

Instantiate form2 class in form1’s button click event handler as below:

private void button1_Click(object sender, System.EventArgs e)
    Form2 frm=new Form2(textBox1.Text);

The Object Approach

Objects are reference types, and are created on the heap, using the keyword new. Here we are going to pass data using objects. The approach is simple; in form2 we are going to instantiate form1 class.

Then instantiate form2 in the button click event handler of form1. After this we are going to pass form1 object to the form2 using form2’s form1 object. The last step is to invoke the form2 window by calling the form2’s show method.

Follow the below steps:

Step 1

Change the access modifier for textbox in form1 to public:

public class Form1 : System.Windows.Forms.Form
 public System.Windows.Forms.TextBox textBox1;

Step 2

In the button click event-handler, add the following code:

private void btnSend_Click(object sender, System.EventArgs e)
    Form2 frm= new Form2();

Step 3

In form2.cs, instantiate form1 class:

public class Form2 : System.Windows.Forms.Form
     private System.Windows.Forms.Label label1;
     public Form1 frm1;

Step 4

In Form2’s Load method, type cast the object (frm1) of form1 to Form1 and access form1’s textbox and assign its text to label’s text.

private void Form2_Load(object sender, System.EventArgs e)

The Properties Approach

Properties allow clients to access class state as if they were accessing member fields directly, while actually implementing that access through a class method. In this method, we are going to add one property to each form. In form1 we are going to use one property for retrieving value from the textbox and in form2, one property to set the label’s text property. Then, in form1’s button click event handler, we are going to instantiate form2 and use the form2’s property to set the label’s text.

Follow the below steps:

Step 1

Add a property in form1 to retrieve value from textbox:

public string _textBox1
    get{return textBox1.Text;}

Step 2

Add a property in form2 to set the labels’ text:

public string _textBox

Step 3

In form1’s button click event handler, add the following code:

private void button1_Click(object sender, System.EventArgs e)
     Form2 frm=new Form2();

The Delegates Approach

Technically, a delegate is a reference type used to encapsulate a method with a specific signature and return type. You can encapsulate any matching method in that delegate. Here we are going to create a delegate with some signature and assign a function to the delegate to assign the text from textbox to label.

Follow the below steps:

Step 1

Add a delegate signature to form1 as below:

public delegate void delPassData(TextBox text);

Step 2

In form1’s button click event handler, instantiate form2 class and delegate. Assign a function in form2 to the delegate and call the delegate as below:

private void btnSend_Click(object sender, System.EventArgs e)
    Form2 frm= new Form2();
    delPassData del=new delPassData(frm.funData);

Step 3

In form2, add a function to which the delegate should point to. This function will assign textbox’s text to the label:

public void funData(TextBox txtForm1)
    label1.Text = txtForm1.Text;


These four approaches are very simple in implementing data passing between forms. There are also other methods available in accomplishing the same. Source code for the methods I stated above is given at the top for download. It is time for you to put on your thinking cap and find other ways of doing this. Happy coding!!!


  • 16th May, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Thiagarajan Alagarsamy
Web Developer
India India
Thiagu is living in Bangalore, India. He has started coding when he was 12 years old. His native is Madurai, a historic city in south India. He loves to code in C#. He frequents code project when he is not coding. Thiagu loves reading Dan Brown and Michael Crichton novels. He is very much interested in Artificial Intelligence (AI). To view his blog -

You may also be interested in...

Comments and Discussions

QuestionI haven't really understood about "delegate", please help me more? Pin
Andrewpeter17-Aug-12 18:45
memberAndrewpeter17-Aug-12 18:45 
GeneralMy vote of 5 Pin
bechtinger20024-Aug-12 10:59
memberbechtinger20024-Aug-12 10:59 
GeneralMy vote of 5 Pin
A.J Bosch12-Jul-12 20:45
memberA.J Bosch12-Jul-12 20:45 
QuestionOpening an existing form Pin
srilathameka12-Jun-12 18:21
membersrilathameka12-Jun-12 18:21 
QuestionIs there a way without always opening a new instance. Pin
vlad7818-Jun-12 12:09
groupvlad7818-Jun-12 12:09 
GeneralAdvantages and Disadvantages Pin
stixoffire26-May-12 22:37
memberstixoffire26-May-12 22:37 
QuestionWhats the significance of having the delegate method than the rest of the other 3 methods ? Pin
Lyricist20-Apr-12 17:44
memberLyricist20-Apr-12 17:44 
GeneralMy vote of 5 Pin
manoj kumar choubey9-Apr-12 18:31
membermanoj kumar choubey9-Apr-12 18:31 
GeneralMy vote of 5 Pin
GuyThiebaut11-Feb-12 22:13
memberGuyThiebaut11-Feb-12 22:13 
GeneralMy vote of 5 Pin
IrineK16-Dec-11 6:03
memberIrineK16-Dec-11 6:03 
GeneralMy vote of 2 Pin
Mayank Topiwala9-Sep-11 23:50
memberMayank Topiwala9-Sep-11 23:50 
GeneralMy vote of 5 Pin
Jonathan Nappee31-Aug-11 0:50
memberJonathan Nappee31-Aug-11 0:50 
Question6 days i am searching for pass the data between forms Pin
dinesh samaria20-Aug-11 1:25
memberdinesh samaria20-Aug-11 1:25 
GeneralMy vote of 1 Pin
iran_girl12-Jul-11 1:24
memberiran_girl12-Jul-11 1:24 
GeneralMy vote of 5 Pin
a1master.moon5-May-11 2:45
membera1master.moon5-May-11 2:45 
GeneralMy vote of 5 Pin
charles henington19-Apr-11 12:52
membercharles henington19-Apr-11 12:52 
GeneralMy vote of 3 Pin
gopinath198923-Dec-10 19:50
membergopinath198923-Dec-10 19:50 
GeneralPassing two or many values Pin
Edward0924-Nov-10 1:30
memberEdward0924-Nov-10 1:30 
GeneralRe: Passing two or many values Pin
Comfyasabadger5-Jan-11 1:21
memberComfyasabadger5-Jan-11 1:21 
GeneralMy vote of 5 Pin
RockingDownTheHighway9-Nov-10 6:53
memberRockingDownTheHighway9-Nov-10 6:53 
GeneralMy vote of 5 Pin
Pritesh Aryan19-Oct-10 20:54
memberPritesh Aryan19-Oct-10 20:54 
GeneralRe: My vote of 5 Pin
Pritesh Aryan11-Feb-11 1:47
memberPritesh Aryan11-Feb-11 1:47 
GeneralRe: My vote of 5 Pin
RaviSant28-Mar-11 22:35
memberRaviSant28-Mar-11 22:35 
GeneralRe: My vote of 5 Pin
Pritesh Aryan28-Mar-11 22:45
memberPritesh Aryan28-Mar-11 22:45 
GeneralVery Helpful Pin
dpminusa29-Sep-10 9:47
memberdpminusa29-Sep-10 9:47 
GeneralPassing Objects With out Closing or Creating Forms Pin
Cameron1inm2-Jul-10 9:16
memberCameron1inm2-Jul-10 9:16 
GeneralAbout Multiple Forms in This Project Pin
avisal18-Feb-10 6:50
memberavisal18-Feb-10 6:50 
GeneralRe: About Multiple Forms in This Project Pin
vlad7818-Jun-12 12:54
groupvlad7818-Jun-12 12:54 
GeneralThanks .. Pin
raindeepak4u30-Nov-09 16:39
memberraindeepak4u30-Nov-09 16:39 
Questionsend variable "counter" from form2 to form1 without creating new form! Pin
Member 40706414-Jun-09 11:17
memberMember 40706414-Jun-09 11:17 
AnswerRe: send variable "counter" from form2 to form1 without creating new form! Pin
m_qaoud2023-Aug-09 23:08
memberm_qaoud2023-Aug-09 23:08 
Generalpassing arrays Pin
srihari_23016-Apr-09 23:36
membersrihari_23016-Apr-09 23:36 
GeneralRe: passing arrays Pin
Thiagarajan Alagarsamy16-Apr-09 23:54
memberThiagarajan Alagarsamy16-Apr-09 23:54 
GeneralThanks Pin
AbdullahMansor4-Jan-09 19:39
memberAbdullahMansor4-Jan-09 19:39 
GeneralNice Pin
veerabagu30-Nov-08 19:50
memberveerabagu30-Nov-08 19:50 
GeneralWell done Pin
Luigi Cordova28-Apr-08 1:22
memberLuigi Cordova28-Apr-08 1:22 
Generalor possibly without the button send feature Pin
isammasri27-Sep-07 14:51
memberisammasri27-Sep-07 14:51 
QuestionCan this be done with multiple textbox fields in a form Pin
isammasri27-Sep-07 14:49
memberisammasri27-Sep-07 14:49 
GeneralThere is a much easier way of doing this... Pin
ayayalar1-Aug-07 11:02
memberayayalar1-Aug-07 11:02 
GeneralRe: There is a much easier way of doing this... Pin
tmom13-Oct-07 9:13
membertmom13-Oct-07 9:13 
GeneralNice one ! Pin
silveroxc24-May-07 3:17
membersilveroxc24-May-07 3:17 
GeneralThat was a Nice Work Pin
venkat_india8714-Apr-07 1:18
membervenkat_india8714-Apr-07 1:18 
GeneralLittle Wrong Pin
Mythran7-Dec-06 5:46
memberMythran7-Dec-06 5:46 
GeneralRe: Little Wrong Pin
Thiagarajan Alagarsamy7-Dec-06 18:34
memberThiagarajan Alagarsamy7-Dec-06 18:34 
Generalnice article but... Pin
TheCardinal1-Nov-06 0:31
memberTheCardinal1-Nov-06 0:31 
AnswerRe: nice article but... Pin
Thiagarajan Alagarsamy2-Nov-06 1:12
memberThiagarajan Alagarsamy2-Nov-06 1:12 
GeneralRe: nice article but... Pin
TheCardinal2-Nov-06 16:03
memberTheCardinal2-Nov-06 16:03 
GeneralNice Pin
morteza5718-Jun-06 23:26
membermorteza5718-Jun-06 23:26 
GeneralNice one Pin
Venkat Eswaran16-May-06 20:45
memberVenkat Eswaran16-May-06 20:45 
GeneralRe: Nice one Pin
Thiagarajan Alagasamy17-May-06 0:15
memberThiagarajan Alagasamy17-May-06 0:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 16 May 2006
Article Copyright 2006 by Thiagarajan Alagarsamy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid