In this article i will show you how you can pass value from one form to another windows form to another windows Form. At the end of the article you will learn to pass value as we pass value in asp.net by using session , request query string.
For this first you needed to create a windows project and add two windows form. In form one add a text box and button and in second form add a label as shown below.
Form1:
Form2:
Now open the
form2 and create a public variable.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Form_Value_passing
{
public partial class Form2 : Form
{
public string _valueOfForm1 = "";
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
lblvalue.Text = _valueOfForm1;
}
}
}
Check the bold text in above code.
Now cone to
form1 and double click on button control to generate the button click event. Now on button click add below code.
Form2 objForm2 = new Form2();
objForm2._valueOfForm1 = txtvalue.Text;
objForm2.Show();
this.Hide();
Now just check the bold section in the code. You are looking that the _valueOfForm1 is the variable which we have declared at form2.
(
Note :In above code first we have assigned the to public variable and then show the form2 and at last hide to form1.)
Just check the below image

Now run the application and add some text after adding text click on button. Form2 will open and you will get the value form1 text box value at form2.

In above you are assigning the collected value to label. Now check the output.

click on button and your output.

________________________________________________________________________
DOWNLOAD________________________________________________________________________