How to Transfer Information between Forms in C#

By FoxLearn 11/21/2024 1:40:14 PM   5.48K
Transferring data between forms in a C# WinForms application can be achieved using several methods.

How to Transfer data between forms in C#?

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "TransferInformationExample" and then click OK

Drag and drop the Label, TextBox and Button controls from the Visual Toolbox onto your form designer, then design your form as shown below.

Form1

tranfer information between form in c#

Form2

transfer information between form in c#

Set public properties in the second form and assign values from the first form before showing it

For example, Using Public Properties

Form1.cs

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 TransferInformationExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRetrieve_Click(object sender, EventArgs e)
        {
            //Open form 2, then retrieve value
            using(Form2 frm = new Form2())
            {
                if (frm.ShowDialog() == DialogResult.OK)
                    txtValue.Text = frm.GetValueInForm2;
            }
        }
    }
}

Form2.cs

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 TransferInformationExample
{
    public partial class Form2 : Form
    {
        //Get value from textbox
        public string GetValueInForm2
        {
            get
            {
                return txtValue.Text;
            }
        }

        public Form2()
        {
            InitializeComponent();
        }
    }
}

For example, Passing Data Through Constructor

You can also pass data to the second form via its constructor when creating an instance of it.

Form1.cs

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    // Pass data via constructor
    private void btnOpenForm2_Click(object sender, EventArgs e)
    {
        string input = txtInput.Text; // Data from Form1
        using(Form2 form2 = new Form2(input)){
            form2.ShowDialog();
        }
    }
}

Form2.cs

public partial class Form2 : Form
{
    public Form2(string text)
    {
        InitializeComponent();
        lblDisplay.Text = text; // Use the received data
    }
}

Create a shared static class or use static variables for data that multiple forms can access.

For example, Using a Shared Class or Static Variables

Common.cs

public static class Common
{
    public static string Data { get; set; }
}

Form1.cs

private void btnOpenForm2_Click(object sender, EventArgs e)
{
    Common.Data = txtInput.Text; // Store data
    Form2 form2 = new Form2();
    form2.Show();
}

Form2.cs

private void Form2_Load(object sender, EventArgs e)
{
    lblDisplay.Text = Common.Data; // Retrieve data
}

Use events to notify and send data between forms dynamically.

For example, Using Events or Delegates

Form2.cs

public partial class Form2 : Form
{
    public delegate void DataSentHandler(string data);
    public event DataSentHandler DataSent;

    private void btnSendBack_Click(object sender, EventArgs e)
    {
        DataSent?.Invoke(txtInput.Text); // Send data to Form1
        this.Close();
    }
}

Form1.cs

private void btnOpenForm2_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.DataSent += Form2_DataSent; // Subscribe to the event
    form2.Show();
}

private void Form2_DataSent(string data)
{
    lblDisplay.Text = data; // Receive and display data from Form2
}

VIDEO TUTORIAL