First define two forms, one for form1 and the other for form2, where form1 serves as the parent window and form2 serves as the Child Window.
1. Pass the value of the parent window to the Child Window
Form1Code:
Public form1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
Form2 lform = new form2 (); // instantiate a form2 window
Lform. string1 = "successfully passed the value to the subwindow form2"; // set the value of string1 in form2
Lform. setvalue (); // set label1's
Lform. showdialog ();
}
Code in form2:
Private string string1;
Public String string1
{
Set
{
String1 = value;
}
}
Public void setvalue ()
{
This. label1.text = string1;
}
Public form2 ()
{
Initializecomponent ();
}
2. Return the Child Window value to the parent window
Code in form1:
Public form1 ()
{
Initializecomponent ();
}
private string strvalue;
Public String strvalue
{< br> set
{< br> strvalue = value;
}< BR >}
Private void btnshowform2_click (Object sender, eventargs E)
{
Form2 lform = new form2 ();
Lform. Owner = This; // an important step, mainly to direct the form2 owner pointer to form1
Lform. showdialog ();
MessageBox. Show (strvalue); // display the returned value
}
Code in from2:
Public form2 ()
{
Initializecomponent ();
}
private void btnclose_click (Object sender, eventargs e)
{< br> form1 lform1 = (form1) This. owner; // assign the form2 parent window pointer to lform1
lform1.strvalue = "subwindow form2 returns a successful value"; // assign a value using the parent window pointer
This. close ();
}