How to transfer data from one form to another form by passing values to form object ?

There are basically three steps to send the data to transfer data from one form to another:-

Step 1 )

a ).   Firstly open the visual studio.
b). Now select file and open project






c ) .Select the visual c# from right side and select the “windows forms application”.

d ). You can change Name of the form as well as location and solution name.

e ). Now click ok button.

f ).  In this demo we want to send name and class from the form one to another form.

g ). Make the form as given below.



Step 2.


In this step we will add new form to the project.

a ).    Left click on project and select “ADD WINOWS FORMS”. A new window open looks like below.


b ). Select windows form and click add. You can change the name as in our case is “form2.cs”

c ). In this, we will take two labels in which we want to show the send values from First form.

It will look like


Step 3.

a). Open first form by selecting the “form1.cs [design]” tab.

b). And double click the button “send values”.

 c). And curser will be in with in the two curly braces of

d). Now add the following coding in between the curly braces.

Form2 form = new Form2(textBox1.Text, textBox2.Text); //this will send the value to the form 2

this.Hide(); // for hiding the form 1

form.Show(); //for opening the form 2

e). Now open the form2 and double click on form2. It will look like

f). Change it and make it like.


public Form2(string nameValue, string classvalue) // this will accept values from the form 1

{
InitializeComponent();

label1.Text = nameValue; // this will store the value in the label1 text

label2.Text = classvalue; // this will store the value in the label1 text

}

g ). This was last to transfer the data. Now save it and run by pressing the shortcut keys ctrl+F5. Your project will run and open first form write in the textboxes and click the button “send values”. New window will open showing the Name and class that you have written in the textboxes

Notice : Now Please click on the below or right hand side Adds .

Post a Comment