SoFunction
Updated on 2025-04-04

C# implements the value transmission between windows

In order to solve the problem of passing values ​​between multiple windows, we can realize the transfer of values ​​between windows by setting static classes and static variables

Form code

//Form 1 codeusing System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
 
namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
     
    private void button1_Click(object sender, EventArgs e)
    {
       = (); //Usage of static variables: class name. Variable name assigns value to static variables      Form2 frm2 = new Form2();
      ();     
    }
  }
  public static class sharedclass //Set a static class sharedclass in the namespace and do not place it in front of form1  {
    public static string sharedvalue; //Set a static variable sharedvalue  }
}

Form 2 code

//Form 2 codeusing System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
 
namespace WindowsFormsApplication1
{
  public partial class Form2 : Form
  {
    public Form2()
    {
      InitializeComponent();
       = ; //The static variable is passed to the textBox of window 2    }
  }
}

The above is the entire content of this article, I hope you like it.