Value-value summary between Windows Form

xiaoxiao2021-04-07  350

In Windows Form, I summarize four methods: global variables, attributes, form constructors, and delegate.

The first global variable:

This is the easiest, as long as the variable is described as static, directly reference Form1 variables in Form2, the code is as follows:

Define a Static Variable in Form1 Public Static Int i = 9;

The button button in FORM2 is as follows:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

TextBox1.text = form1.i.tostring ();

}

The second method is to use the attribute, please see my blogger details:

Http://blog.9cbs.net/tjvictor/archive/2006/06/04/772711.aspx

The third method is to use constructor:

The Form1's Button button writes this:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

FORM2 TEMP = New Form2 (9);

TEMP.SHOW ();

}

The constructor of form2 writes this:

Public Form2 (INT i)

{

InitializationComponent ();

TextBox1.text = i.tottring ();

}

The fourth method is to use Delegate, the code is as follows:

Delegate first in Form2

Public Delegate Void ReturnValue (INT I);

Public ReturnValue ReturnValue;

The Button button code in FORM2 is as follows:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

IF (ReturnValue! = NULL)

RETURNVALUE (8);

}

The Button button in Form1 is as follows:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

FORM2 TEMP = New Form2 ();

Temp.ReturnValue = New Temp.form2.ReturnValue (showValue);

TEMP.SHOW ();

}

Private Void ShowValue (INT i)

{

TextBox1.text = i.tottring ();

}

Click on the button of Form2, the value in the textbox in Form1 will change accordingly.

In these four methods,

The first is two-way passages, that is, Form1 and Form2 change the value of i, and the other will be affected.

The second method can be two-way by two ways.

The third method is Form1-> Form2 unidirectional value.

The fourth method is FORM2-> Form1 one-way passage.

I have a new way to add new ways, and one is Event, and it is similar to delegate. I don't say it here.

转载请注明原文地址:https://www.9cbs.com/read-132576.html

New Post(0)