Pass value between two form windows (parent> child) (Child> parent)

Source: Internet
Author: User

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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.