SoFunction
Updated on 2025-03-06

c# Several ways to pass values ​​between WinForm forms (summary)

Preface

The editor recently maintained a Winfrom form, which is the main form of the CS side in the project. Many sub-forms need to obtain values ​​from the main form, and the sub-forms also need to pass values ​​back to the main form. Let me introduce them to you below.

text

In this article, the main form is frmMain, the child form is frmGroup, and the value passed between the two forms is used as an example.

Method 1: Use public static variables to pass values

Code in main form frmMain

public partial class frmMain : Form
{
 //Declare the station ID as a public static variable public static string terminalID = "";
 // Assign a value to a static variable terminalID = "q13bh01-bh12";
 }

Code in child form frmGroup

 private void frmGroup_Load(object sender, EventArgs e)
{
  = (); 
  //You can assign values ​​to static members to facilitate calls to other forms   = "q13bh01-bh11";
}

Features: Two-way value transmission, simple implementation
Disadvantages: Static variables are allocated memory when the class is loaded and stored in the method area. They are generally not destroyed. When the system does not have enough memory, static memory will be automatically recycled, which will cause global static errors to access.

Method 2: Use public variables to pass values

Code in main form frmMain

public partial class frmMain : Form
{
 //Declare the station ID as a public variable public string terminalID = "";
 // Assign a value to the variable terminalID = "q13bh01-bh12";
 // When clicking the 'Behavior' button, the value will be passed to the form private void btnGroup_Click(object sender, EventArgs e)
    {
      frmGroup frmGro = new frmGroup();
      //Transfer the value of the variable, please note that the order is written before the ShowDialog() method      frmGro .stationID = ;
      frmGro .ShowDialog();
    }
 }

Code in child form frmGroup

 public partial class frmGroup : Form
 {
  //Define public attributes  public string stationID = "";
 }

Features: One-way transmission value, only the main form can pass value to the child form, which is simple to implement.

Method 3: Use delegate to pass values

Let's first look at the code in the subform frmGroup

namespace 
{
  //1. Declare a delegation  public delegate void setTextValue(string textValue,bool flag);
  public partial class frmGroup : Form
  {
    //2. Declare an event of delegate type     public event setTextValue setFormTextValue;
     public string groupName = "";
     public bool flagBtnGroup = false; 
     public frmGroup()
     {
      InitializeComponent();
     }
     //Poll the 'behavior' button (equivalent to the button click event)     private void tmrBtn_Tick(object sender, EventArgs e)
     {
      if (sender is ButtonX) {
        ButtonX butX = (ButtonX)sender;//Judge whether the trigger event is the Button        groupName = ();
        flagBtnGroup = true;
        //3. Prepare the data to be returned.        setFormTextValue((" ", ""),  );
        ();
        return;
      } 
    }

Code in main form frmMain

 private void btnGroup_Click(object sender, EventArgs e)
    {
      frmGroup frmGro = new frmGroup();
       //4. Initialize event      frmGro .setFormTextValue += new setTextValue(frmGro _setFormTextValue);
      //Transfer the value of the variable, please note that the order is written before the ShowDialog() method      frmGro .stationID = ;
      frmGro .ShowDialog();
    }
    //5. The specific implementation of the event     public void frmGro _setFormTextValue(string textValue,bool flag)
    { 
       = textValue;
       = flag;
      if (!(newGroupName))
      {
         ……
      }
     }

Features: Suitable for real-time return of child form data to the parent form.

Method 4: Pass a numeric value using the constructor

class FormB
{
 int orgId;
 public FormB(int orgId)
 {
   =orgId;
 }
}

class FormA
{
public void ShowB()
{
  FormB fb=new FormB(5); // 5 is to pass the past value  ();
}
}

Method 5: Pass the entire A form to the B form

Sometimes it is necessary to modify a certain control value of Form A in Form B, and immediately change the control display of Form A. This method is suitable for this situation.

Modify FormA, set the accessibility of the control to be processed in B to public, and then process it as follows

class FormA
{
public void ShowB()
{
  FormB fb=new FormB(this); // Pass the A form itself in the constructor  ();
}
}

class FormB
{
FormA fm; // Add a FormA member of the FormA typepublic FormB(FormA fm) //Add a constructor with parameters, the parameter type is FormA{
  =fm;
}
protected void Method1()
{
  ="Haha!"; //You can directly operate the controls in Form A here!}
}

A few words

In the project, the parent form passes the value to the child form and passes the fixed value. The first method is used. The child form passes the value to the parent form and passes the changed value. The third method is used. The third method solves a big problem for me.

Summarize

Thank you for your reading. There are many ways to pass the value in the form, such as constructing parameter parameter transmission, public attribute transmission, etc., and finding the best suitable for the needs in various scenarios. I hope to give the editor valuable suggestions!

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.