Data transfer one form to another in c#

In this tutorial we will learn how to pass data from one form to another form in Windows Forms applications using C#.

Let's create windows form.

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

create windows form

Step 2
Enter the username and password in textboxes and click the submit button.

Output of login page

Step 3
I use two global static declare username,password

static string username, password;

public static string Username
{
    get { return Form1.username; }
    set { Form1.username = value; }
}

public static string Password
{
    get { return Form1.password; }
    set { Form1.password = value; }
}

If you don't know the global variable are used then click here

Step 4
When submit button click then textBoxusername,textBoxpassword text property set the username,password properties. And show the Form2 windows form.

private void btnSubmit_Click(object sender, EventArgs e)
{
    username = textBoxusername.Text;
    password = textBoxpassword.Text;
    Form2 f2 = new Form2();
    f2.Show();
}

Step 5
Getting the username and password in the previous form. And show the data in lblusername and lblpassword.

Second windows form

Step 6
When Form2 are loading then the previous form's value getting.

lblusername.Text +=" "+ Form1.Username;
lblpassword.Text +=" "+ Form1.Password;

Step 7
Show the output

Second windows form

Write a comment


Name Email Message