SoFunction
Updated on 2025-03-07

Methods of passing values ​​between WinForm forms

This article describes the method of passing values ​​between WinForm forms. Share it for your reference. The specific implementation method is as follows:

Passing data between forms, whether it is the parent form that operates the child form or the child form operator form, there are several ways:
 
1. Public static variables;
2. Use common attributes;
3. Use delegation and events;
4. Pass the main form to the slave form through the constructor;

1. Pass static variables

Features: The value transmission is bidirectional, and the implementation is simple
 
The implementation code is as follows:
Define a static member value in an app class

Copy the codeThe code is as follows:
public class app
{
public static string value;
}

Call this in Form1
Copy the codeThe code is as follows:
= "f2"; // Assign a value to a static member
new Form2 ( ).Show ( ); //Show Form2

In Form2
Copy the codeThe code is as follows:
= ; //Retrieved value
= "Form2"; // Assign a value so that other forms can be called

 
2. Public variables pass values

This method is to use a common variable, first store the required value in this common variable, and then use it by reading the value of the variable when needed.
example
Form1:

Copy the codeThe code is as follows:
public static string Form1 Value; // Note that it must be declared as a static variable
private void button1_Click(object sender, EventArgs e)
{
Form1Value = "from Form1";
new Form2().Show();
}

Form2:
Copy the codeThe code is as follows:
private void Form_Load(object sender, EventArgs e)
{
(Form1.Form1Value);
}

This way of passing values ​​is relatively simple in understanding and use, but it is easy to make variable string values, such as modifying them to "a" for the first time and modifying them to "b" for the second time, it is possible that the result of the value of "a" originally needed becomes "b".
 
3. Static method access

This method is somewhat similar to the first method of passing value, which is to define methods that need to be accessed by other forms with static so that other passes can directly access
example:
Form1:

Copy the codeThe code is as follows:
private void button1_Click(object sender, EventArgs e)
{
new Form2().Show();
}
public static void FF()
{
("Form1 method");
}

Form2:
Copy the codeThe code is as follows:
private void Form_Load(object sender, EventArgs e)
{
();
}

Using this method to access other forms is convenient to cross-form, but if you need to access the control value, you cannot directly access it. You need to pass the value to other forms first, and then pass the form back, or store it in other variables to access this variable.
 
4. Public property values ​​through the form

Features: Simple implementation
 
The implementation code is as follows:
Define a public property Form2Value in Form2, get and set the text value of textBox1 in Form2

Copy the codeThe code is as follows:
public string Form2Value
{
get
{
return this.;
}
set
{
this. = value;
}
}

Call this in Form1
Copy the codeThe code is as follows:
Form2 f2 = new Form2 ( );
f2.Form2Value = "Ok"; // Assign Ok to the textBox1 of Form2
( );

 
5. Through the form's public attribute value and Owner attribute

Features: Simple and flexible implementation
 
The implementation code is as follows:
In Form1

Copy the codeThe code is as follows:
public int Form1Value = 1;
Form2 f2 = new Form2 ( );
( this ); //Pause Form1 as the owner of Form2 to Form2

In Form2
Copy the codeThe code is as follows:
//The owner of Form2 is Form1
Form1 f1 = ( Form1 ) ;
//The value of Form1 is 1
( f1.Form1Value .ToString ( ) );
// Assign a value of 222 to Form1Value of Form1
f1.Form1Value = 222;

 
6. Public property values ​​and properties through the form

Description: Properties: Get the collection of open forms belonging to the application. (This property is in .NET Framework version 2.0)
 
The implementation code is as follows:
In Form1

Copy the codeThe code is as follows:
public int Form1Value = 1;
Form2 f2 = new Form2 ( );
( );

In Form2
Copy the codeThe code is as follows:
string formName = "Form1";
Form fr = [ formName ];
if ( fr != null )
{
Form1 f1 = ( Form1 ) fr;
//The value of Form1 is 1
( f1. ( ) );
// Assign a value of 222 to Form1Value of Form1
f1.Form1Value = 222;
}

 
7. Transfer value

As the name suggests, this method is to pass the required value-argument to the form of the required value-argument
example:
Form1:

Copy the codeThe code is as follows:
private void button1_Click(object sender, EventArgs e)
{
new Form2("from Form1").Show();
}

Form2:
Copy the codeThe code is as follows:
public Form2(string value)
{
InitializeComponent();
(vaue);
}

This type of inter-form value transfer method is obviously better than the first type of value transfer parameters, and there will be no data string phenomenon. However, you need to pay attention to modifying the Form2 constructor. The default constructor of each form is parameterless by default, so you need to modify the constructor.
 
8. By constructing functions

Features: The value is one-way (the value cannot be passed to each other), and the implementation is simple
 
The implementation code is as follows:
In Form2

Copy the codeThe code is as follows:
int value1;
string value2;
public Form2 ( int value1 , string value2 )
{
InitializeComponent ( );
this.value1 = value1;
this.value2 = value2;
}

Call this in Form1
Copy the codeThe code is as follows:
new Form2 ( 111 , "222" ).Show ( ); //In this way, the 2 values ​​of 111, "222", are transmitted to Form2

 
9. Use commission to achieve it.

Delegation can bring one method as a parameter into another method. In the form passing value, the child form needs to execute a method to change the value of the parent form.
This method can be passed from the parent form using delegate. In the parent form, declare the method to modify the text box AfterChildChange, and pass this method to the child form when new a child form. Then when the child form clicks the synchronization button, the AfterChildChange method of the parent form will be executed, which has achieved the purpose of modifying the text box value.
 
example
1. Set a delegate type attribute in the subform:

Copy the codeThe code is as follows:
public Action<string> AfterChangeTextDel { get; set; }

2. In the subform synchronization button:
Copy the codeThe code is as follows:
if (AfterChangeTextDel != null)
{
AfterChangeTextDel(this.);//Execute delegation
}

3. Add method to the parent form:
Copy the codeThe code is as follows:
public void AfterChildChange(string text)
{
= text;
}

4. Launch child form button in parent form:
Copy the codeThe code is as follows:
ChildFrm frm =new ChildFrm();
= new Action<string>();
();

5. This can also implement the form passing value, and the child form's delegation can be directly executed elsewhere in the parent form. : Solve this issue. Microsoft introduced events.
 
10. Use events to achieve

Events are objects of delegate type. It is implemented internally by delegating. For events, the outside world can only register itself += and cancel itself -=. The outside world cannot cancel other registrants, nor can it actively trigger events. The delegate cannot achieve these controls, so the general syntax of the event was born.
 
The implementation code is as follows
Define the public property Form2Value in Form2, get and set the text value of textBox1 in Form2
And also define an accept event

Copy the codeThe code is as follows:
public string Form2Value
{
get
{
return this.;
}
set
{
this. = value;
}
}
public event EventHandler accept;
 
private void button1_Click ( object sender , EventArgs e )
{
if ( accept != null )
{
accept ( this , ); //When the form triggers an event, pass its own reference
}
}

In Form1
Copy the codeThe code is as follows:
Form2 f2 = new Form2 ( );
+= new EventHandler ( f2_accept );
( );
void f2_accept ( object sender , EventArgs e )
{
//The receiver of the event gets a reference to Form2 through a simple type conversion
Form2 f2 = (Form2) sender;
//Received Form2
this. = f2.Form2Value;
}

I hope this article will be helpful to everyone's C# programming.