// 1 global variable
// This is the simplest. You only need to describe the variable as static. In form2, the variable form1 is directly referenced. The Code is as follows:
// Define a static variable public static int I = 9 in form1;
// The button in form2 is as follows:
Private void button#click (Object sender, system. eventargs E)
{Textbox1.text = form1. I. tostring ();}
// 2 pass the value of the parent window to the Child Window (form1 is the main form, and form2 is the child form)
// The Code is as follows:
// Code in form1:
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void btnshow_click (Object sender, eventargs E)
{
Form2 form2 = new form2 ();
// Pass the value of the parent form to the child form
Form1 form1 = new form1 ();
Form2.string1 = "successfully passed the value to the form2 form! ";
Form2.setvalue ();
Form2.showdialog ();
}
// Code in form2:
Private string;
Public String string1
{
Get {return string ;}
Set {string = value ;}
}
Public void setvalue ()
{
This. lbltext. Text = string1;
}
// 3. Pass the value of the Child Window to the parent window (form1 is the main form, and form2 is the child form)
// Code in form1:
Private string strvalue;
Public String strvalue
{
Get {return strvalue ;}
Set {strvalue = value ;}
}
Private void btnshow_click (Object sender, eventargs E)
{
Form2 form2 = new form2 ();
// Return the child form value to the parent form
Form2.owner = This; // an important step, mainly to direct the form2 owner pointer to form1
Form2.showdialog ();
MessageBox. Show (strvalue); // display the returned value
}
// Code in form2:
Private void btnclose_click (Object sender, eventargs E)
{
Form1 form1 = (form1) This. owner; // assign the form2 parent window pointer to lform1
Form1.strvalue = "the child form returns a successful return value to the parent form! "; // Assign values using the parent window pointer
This. Close ();
}
// Fourth:
// It is actually the same as the third method.
Winform1 {};
Winform2 {public winform1 OWF = NULL ;}