WinForm method for passing values between forms

  • 2020-11-26 18:57:49
  • OfStack

This article illustrates the WinForm method of passing values between forms. Share to everybody for everybody reference. Specific implementation methods are as follows:

There are several ways to pass data between forms, whether a parent form operator or a child form operator:

1. Public static variables;
2. Use common attributes;
3. Use delegates and events;
4. Pass the main form to the slave form through the constructor;

1. Through static variables

Features: Value transfer is bidirectional, easy to implement

The implementation code is as follows:
Define 1 static member value in 1 app class

public class app
{
public static string value;
}

This is called in form Form1
app.value = "f2"; // Assign a value to a static member 
new Form2 ( ).Show ( ); // According to Form2

In Form Form2
this.Text = app.value; // To retrieve app.value The value of the 
app.value = "Form2"; // to app.value Assign values for other forms to call


2. Value passing of public variables

The method is to take a public variable, store the required value in the public variable, and then use it by reading the value of the variable when needed
example
Form1:

public static string Form1Value; //  Note that it must be stated as static variable 
private void button1_Click(object sender, EventArgs e)
{
Form1Value = " from Form1";
new Form2().Show();
}

Form2:
private void Form_Load(object sender, EventArgs e)
{
MessageBox.Show(Form1.Form1Value);
}

It is relatively simple to understand and use the value in this way, but it is easy to make the variable string value. For example, if the value of "a" is changed in the first time and "b" is changed in the second time, it is possible that the value of "a" is changed into "b".

3. Static method access

This approach is somewhat similar to the first method of passing values, in that methods that need to be accessed by other forms are defined in static so that other passes can access them directly
Example:
Form1:

private void button1_Click(object sender, EventArgs e)
{
new Form2().Show();
}
public static void FF()
{
MessageBox.Show("Form1 methods ");
}

Form2:
private void Form_Load(object sender, EventArgs e)
{
Form1.FF();
}

Access to other forms using this method is convenient across forms, but the value of the control cannot be accessed directly if it needs to be accessed. The value needs to be passed to another form first, then passed back to the form, or stored after another variable to access the variable.

4. Through the public property values of the form

Features: Simple implementation

The implementation code is as follows:
Define a public property Form2Value on form Form2 to get and set the text value of textBox1

public string Form2Value
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value;
}
}

This is called in form Form1
Form2 f2 = new Form2 ( );
f2.Form2Value = "Ok"; // to Form2 the textBox1 The assignment Ok
f2.ShowDialog ( );


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

Features: Simple implementation, flexible

The implementation code is as follows:
On Form Form1

public int Form1Value = 1;
Form2 f2 = new Form2 ( );
f2.ShowDialog ( this ); // the Form1 As a Form2 Is passed to Form2

In Form Form2
//Form2 The owner of Form1
Form1 f1 = ( Form1 ) this.Owner;
// Get to the Form1 The value is 1
MessageBox.Show ( f1.Form1Value .ToString ( ) );
// to Form1 the Form1Value The assignment 222
f1.Form1Value = 222;


6. Through the form's public property value and Application. OpenForms property

Application. OpenForms property: Gets the collection of open forms belonging to the application. (This property is in.NET Framework2.0)

The implementation code is as follows:
On Form Form1

public int Form1Value = 1;
Form2 f2 = new Form2 ( );
f2.Show ( );

In Form Form2
string formName = "Form1";
Form fr = Application.OpenForms [ formName ];
if ( fr != null )
{
Form1 f1 = ( Form1 ) fr;
// Get to the Form1 The value is 1
MessageBox.Show ( f1.Form1Value.ToString ( ) );
// to Form1 the Form1Value The assignment 222
f1.Form1Value = 222;
}


7. Parameter transfer value

This method, as the name implies, passes the desired value 1 parameter form to the desired form
Example:
Form1:

private void button1_Click(object sender, EventArgs e)
{
new Form2(" from Form1").Show();
}

Form2:
public Form2(string value)
{
InitializeComponent();
MessageBox.Show(vaue);
}

This method of passing values between forms is obviously better than the first method of passing values, and there will be no string data phenomenon. However, it needs to be noted that the constructor of Form2 needs to be modified. The default constructor of each form is parameterless by default, so the constructor needs to be modified.

8. Through constructors

Features: the value is one-way (can not be passed to each other), simple implementation

The implementation code is as follows:
In Form Form2

int value1;
string value2;
public Form2 ( int value1 , string value2 )
{
InitializeComponent ( );
this.value1 = value1;
this.value2 = value2;
}

This is called in form Form1
new Form2 ( 111 , "222" ).Show ( ); // Thus the 111,"222", this 2 A value was sent to Form2


9. Do it with a delegate.

Delegate can take 1 method as a parameter to another method. In the form pass value, the child form needs to execute 1 method to change the value of the parent form.
This method, on the other hand, can be passed from the parent form with a delegate. On the parent form, declare the method AfterChildChange for modifying the text box, which is passed to the child form when new1 is a child form. Then when the child form clicks the synchronization button, the parent form's AfterChildChange method will be executed to modify the text box value.

example
1. Set a delegate type property in the child form:

public Action<string> AfterChangeTextDel { get; set; }

2. In the synchronization button of the child form:
app.value = "f2"; // Assign a value to a static member 
new Form2 ( ).Show ( ); // According to Form2
8
3. Add method in the parent form:
app.value = "f2"; // Assign a value to a static member 
new Form2 ( ).Show ( ); // According to Form2
9
4. Start the child form button on the parent form:
this.Text = app.value; // To retrieve app.value The value of the 
app.value = "Form2"; // to app.value Assign values for other forms to call
0
5, this also enables the form to pass values, the child form delegate can be executed directly in the parent form elsewhere. : Solve the problem. Microsoft introduces events.

10. Use events for implementation

An event is an object of the delegate type. It is implemented internally with the delegate. For the event, the external can only register itself += and logout itself -=, while the external can not logout other registrants, nor can it actively trigger the event. Delegates do not implement these controls, hence the event-general syntax.

The implementation code is as follows
Define the public property Form2Value on form Form2 to get and set the text value of textBox1
An accept event is also defined

public string Form2Value
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value;
}
}
public event EventHandler accept;
 
private void button1_Click ( object sender , EventArgs e )
{
if ( accept != null )
{
accept ( this , EventArgs.Empty ); // When the form fires an event, it passes its own reference
}
}

In form Form1
Form2 f2 = new Form2 ( );
f2.accept += new EventHandler ( f2_accept );
f2.Show ( );
void f2_accept ( object sender , EventArgs e )
{
// The receiver of the event passes 1 A simple type cast is given Form2 A reference to the
Form2 f2 = (Form2) sender;
// To receive Form2 the textBox1.Text
this.textBox1.Text = f2.Form2Value;
}

Hopefully this article has helped you with your C# programming.


Related articles: