Method of passing values for C # forms

Source: Internet
Author: User


Learn about the display of the form, and then summarize the form's method of passing the value:

1. Through the characteristics of the constructor: The value is one-way (can not pass the value of each other), the implementation of a simple code is as follows:
In the form Form2

intvalue1;stringvalue2; PublicForm2 (intValue1,stringvalue2)
{InitializeComponent ();
This. value1 = value1;
This. value2 =value2;
This is called in form Form1.NewForm2 (111,"222"). Show ();//This gives the 111, "222", 2 values to the Form22. Static variable characteristics: The pass value is bidirectional, the implementation of simple implementation code is as follows: Define a static member in an app class
Value Publicclassapp
{  PublicStaticstringvalue;
This is called App.value in form Form1.="F2";//assigns a value of new Form2 () to a static member.  Show (); //show Form2 in form Form2 This. Text = App.value;//retrieving the value of the App.valueApp.value ="Form2";//assigns a value to app.value so that other forms call3. Through the form's Public property value feature: Implement the simple implementation code as follows: Define a public property form2value in form Form2, get and set the TextBox1 text value

PublicstringForm2value
{

Get{         return This. TextBox1.Text;
}

Set
{          This. TextBox1.Text =value;
}
This is called in form Form1.
Form2 F2=NewForm2 (); F2. Form2value="Ok";//assign OK F2 to Form2 's textBox1.   ShowDialog (); 4. Through the form's public property values and the Owner property characteristics: The implementation of simple, flexible implementation of the code as follows:
In the form Form1 PublicintForm1value =1;
Form2 F2 =NewForm2 (); F2. ShowDialog ( This);//Pass Form1 as the owner of the Form2 to Form2 in the form Form2
//the owner of the Form2 is Form1Form1 F1= (FORM1)
This. Owner;//the value taken to Form1 is 1.MessageBox.Show (F1. Form1value. ToString ());//Assign 222 F1 to Form1 's form1value. Form1value = 222;

5. Through the form's public property values and Application.openforms Property Description: Application.openforms property: Gets the collection of open forms that belong to the application. (This property is in. NET version Framework2.0) Implementation code is as follows:
In the form Form1 PublicintForm1value =1;
Form2 F2 =NewForm2 ();
F2. Show ();
In the form Form2stringFormName ="Form1"; Form FR= Application.openforms [FormName];

if(FR! =NULL )
{Form1 F1= (Form1) fr;//the value taken to Form1 is 1.MessageBox.Show (F1. Form1value.tostring ());//Assign 222 F1 to Form1 's form1value. Form1value = 222;
}

6The implementation code of the event is as follows: Define the public property form2value in form Form2, get and set the TextBox1 text value and also define an accept event PublicstringForm2value
{
   Get
{         return This. TextBox1.Text;
}

Set
{          This. TextBox1.Text =value;
}
}  PublicEventEventHandler Accept; PrivatevoidButton1_Click (Objectsender, EventArgs e)
{     if(Accept! =NULL)
{Accept ( This, Eventargs.empty);//when a form triggers an event, it passes itself a reference
}
} Form2 F2 in form Form1=NewForm2 (); F2.accept+=NewEventHandler (f2_accept);
F2. Show (); voidF2_accept (Objectsender, EventArgs e)
{ //the receiver of the event gets a Form2 reference by a simple type conversion
Form2 F2 = (Form2) sender; //received the TextBox1.Text of the Form2 This. TextBox1.Text = F2. Form2value;
}

Method of passing values for C # forms

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.