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.
public partial class Form2 : Form
public string _valueOfForm1 = "";
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;
Now just check the bold section in the code. You are looking that the _valueOfForm1 is the variable which we have declared at form2.
: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.