Pass Data from one Form to another Form in Windows Application



Send Data in C# Windows Application

In out previous c# .net post we have learned how to open windows form on button click event and difference about Show() and ShowDialog(). Now, In this windows application tutorial we will learn to pass data from one windows form to another windows form.

Here, we will take an example to see how to pass data in windows application. We have two windows forms Form1 and Form2, we send data from Form1 to Form2 in below example.

We can pass data in two ways, one the data send from one form can be received on multiple (any) form in same application and second way we can receive data on defined single form.

  1. Pass data from one from to multiple form
  2. Pass data from one form to one form

Pass data to multiple form

Here we will take a login page and send username and password to another form in windows application. Here we can receive data on any windows form which we want.

meeraacademy.com
Pass data from one form to another form in windows application.

Here,  we need to declare two public static sting variable for username and password data and assign data to variable as shows in below screen.

meeraacademy.com
Pass data from one form to another form in windows application.

Here is the code of Form1.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyFirstProject
{
 public partial class Form1 : Form
 {
 public static string uname;
 public static string upass;
 public Form1()
 {
   InitializeComponent();
 }
 private void Form1_Load(object sender, EventArgs e)
 {

 }
 private void btnlogin_Click(object sender, EventArgs e)
 {
   uname = txtuname.Text;
   upass = txtpass.Text;
   this.Hide();
   Form frm2 = new Form2();
   frm2.ShowDialog();
 }
 }
}

Now, take two label control on Form2 for display username and password value on Form2. write below code on Form2 page load event for receive and display data on form.

Here is the code of Form2.

public Form2()
{
   InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
   lblusername.Text = Form1.uname;
   lblpassword.Text = Form1.upass;

}

In above example we can received username and password value from any form in our windows application.


Pass data to single form

Let’s understand how to pass data on predefined form in windows application. We will take same above example to understand this concept. In this method we don’t need to declare public variable in Form1.

write below code on Button control click event on Form1.

private void btnlogin_Click(object sender, EventArgs e)
{
   this.Hide();
   Form frm2 = new Form2(txtuname.Text,txtpass.Text);
   frm2.ShowDialog();
}

Here we pass username and password value with Form2 show method.

Here is the code of Form2.

public partial class Form2 : Form
{
string uname;
string upass;
public Form2(string txtname, string txtpass)
{
uname = txtname;
upass = txtpass;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
   lblusername.Text = uname;
   lblpassword.Text = upass;

}
}

Here, we have declare two global string variable for username and password and retrieve value from Form2 and display username and password value on label control in Form2. In above example we can receive data on only Form2.

Out put of above example :

meeraacademy.com
Pass data from one form to another form in windows application.

We hope this c# windows application post will help you to understand how to pass data from one windows form to another windows form.