Code in main form:
Public Partial classform1:form{ PublicForm1 () {InitializeComponent (); } Private voidBtnopen_click (Objectsender, EventArgs e) {//Click the Popup methodFrmchild Frmchild =NewFrmchild (); Frmchild.showdialog (); if(Frmchild.dialogresult = =System.Windows.Forms.DialogResult.OK) {TextBox1.Text= Frmchild.strvalue;//gets the property value of the popup form } }}
The code in the pop-up window:
Public Partial classfrmchild:form{ PublicFrmchild () {InitializeComponent (); } Private stringstrvalue =""; Public stringstrvalue {Get{returnstrvalue;} Set{strvalue =value;} } Private voidBtnok_click (Objectsender, EventArgs e) { //after clicking OKstrvalue = TextBox1.Text;//Assign the value of a text box to the properties of the form This. DialogResult =DialogResult.OK; This. Close (); } }
WinForm form pass-through value
Learn about the display of the form, and then summarize the form's method of passing the value:
1. Through the constructor function
Features: Pass value is unidirectional (can not pass the value of each other), to achieve a simple
The implementation code is as follows:
In the form Form2
int value1;
String value2;
Public Form2 (int value1, string value2)
{
InitializeComponent ();
this.value1 = value1;
This.value2 = value2;
}
This is called in form Form1.
New Form2 (111, "222"). Show (); This gives the 111, "222", 2 values to the Form2
2. Passing Static variables
Features: The pass value is bidirectional, the realization is simple
The implementation code is as follows:
Define a static member in an app class value
public class App
{
public static string value;
}
This is called in form Form1.
App.value = "F2"; Assign a value to a static member
New Form2 (). Show (); Show Form2
In the form Form2
This. Text = App.value; Retrieving the value of the App.value
App.value = "Form2"; Assigns a value to app.value so that other forms call
3. Pass the form's public property value
Features: Simple to implement
The implementation code is as follows:
Define a public property form2value in form Form2, get and set the text value of TextBox1
public string Form2value
{
Get
{
return this.textBox1.Text;
}
Set
{
This.textBox1.Text = value;
}
}
This is called in form Form1.
Form2 F2 = new Form2 ();
F2. Form2value = "OK"; Assign the TextBox1 to Form2 OK
F2. ShowDialog ();
4. Through the form's public property values and the Owner property
Features: simple and flexible implementation
The implementation code is as follows:
In the form Form1
public int form1value = 1;
Form2 F2 = new Form2 ();
F2. ShowDialog (this); Pass the Form1 as the owner of the Form2 to Form2
In the form Form2
The owner of the Form2 is Form1
Form1 f1 = (Form1) this. Owner;
The value taken to Form1 is 1.
MessageBox.Show (F1. Form1value. ToString ());
Assign a value of 222 to Form1 's Form1value
F1. Form1value = 222;
5. Through the form's public property values and Application.openforms properties
Description: Application.openforms property: Gets the collection of open forms that belong to the application. (This property is in. NET version Framework2.0)
The implementation code is as follows:
In the form Form1
public int form1value = 1;
Form2 F2 = new Form2 ();
F2. Show ();
In the form Form2
String formName = "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 a value of 222 to Form1 's Form1value
F1. Form1value = 222;
}
6. Via event
implementation code is as follows:
public string form2value
{
get
{
return this.textBox1.Text;
}
set
{
This.textBox1.Text = value;
}
}
public event EventHandler Accept;
private void Button1_Click (object sender, EventArgs e)
{
if (accept! = null)
{
Accept (this, eventargs.empty); When a form triggers an event, it passes itself a reference
}
}
In the form Form1
Form2 F2 = new Form2 ();
F2.accept + = new EventHandler (f2_accept);
F2. Show ();
void F2_accept (object sender, 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;
}
http://www.cnblogs.com/aierong/archive/2008/11/17/winform.html WinForm Development, form display and form value related knowledge summary
Http://blog.sina.com.cn/s/blog_55da6e9d0102w514.html C # WinForm main form forms the simplest way to get the median value of a popup form
WinForm Development, form display and form value related knowledge summary