C # cross-form pass-through values

Source: Internet
Author: User

The first method:
Create a class that declares the fields that are used to store the received. It is stored in the field when it is used, and is called directly by the class name. (This method of passing is bidirectional)

The second method:
1. Defined in Form1

public string Name = "* * * *"


2. Create the Form1 object in the Form2,

Form1 f = new Form1 ();


And then you can go through F. Name is value.

The third method: Use the constructor function
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 Form2 D.
But this is a one-way pass.

Fourth method: Through the form's public property values (features: simple implementation)
For example, "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 ();


Fifth method: Through the form's public property value and the Owner property (features: simple and flexible implementation)

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;



Sixth method: Through the form's public property value and the Application.openforms attribute (less sense)
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;
}



Seventh method: Pass Event
To define a public property form2value in form Form2, get and set the text value of TextBox1
And also defines an accept event


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

Remaining methods: Database, file, Socket channel, Wm_copydata

C # cross-form pass-through values

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.