Transfer value between winform windows

Source: Internet
Author: User
// 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 ;}

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.