June 28, 2017

Pass Data One Form to Another in Windows Form Application

Introduction

Hi, Friends In this tutorial  I will tell you how you can pass data from one form to another form in Windows Forms applications using C#.

Let's use the following stepsto create a Windows Forms application.

Step 1 
In Visual Studio select "File" -> "New" -> "Project..." then select C# Windows Forms Application then click Ok.




Step 2
Drag and drop a Label and a TextBox and a button from the Toolbox.



Step 3
Use a global variable. And write the following code in the Form1.cs.

public static string SetValueForText1 = ""

Step 4
Add another Windows Forms form using Project --> Add Windows Form then click on Add.



 Step 5
 Now Double-click on the Submit button on the Windows Form1 and write the following code:

 private void btnPassData_Click(object sender, EventArgs e)
        {
            SetValueForText1 = txtData.Text;
            Form2 frm2 = new Form2();
            frm2.Show();  
        }

Step 6
Drag and Drop a label from the Toolbox onto Form2.



Step 7
Double-click on Form2 and write the code:

private void Form2_Load(object sender, EventArgs e)
        {
            label1.Text = Form1.SetValueForText1; 
        }

Step 8
Now press F5 to run the application.

Enter name in  Form1 and click on Submit. The data will pass from Form1 to Form2.





 


No comments:

Post a Comment