(C #) transfer values between winform forms

Source: Internet
Author: User

1. Constructor

Features: it is easy to transmit values in one way (values cannot be transferred to each other ).

ImplementationCodeAs follows:

In 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 (); // in this way, 111, "222", these two values are sent to form2

 

2. Use static variables

Features: The value transfer function is bidirectional and easy to implement.

The implementation code is as follows:

Define a static member value in an app class

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 (); // display form2

In form form2

This. Text = app. value; // retrieves the value of APP. value.

App. value = "form2"; // assign a value to app. value so that other forms can call

 

3. Public attribute values of the form

Features: easy to implement

The implementation code is as follows:

Define a public attribute form2value in the form form2 to 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 OK to textbox1 of form2

F2.showdialog ();

 

4. Public attribute values and owner attributes of the form

Features: simple and flexible

The implementation code is as follows:

In form form1

Public int form1value = 1;

Form2 F2 = new form2 ();

F2.showdialog (this); // pass form1 as the form2 owner to form2

In form form2

// Form2 is owned by form1

Form1 F1 = (form1) This. owner;

// The value of form1 is 1.

MessageBox. Show (f1.form1value. tostring ());

// Assign a value of 222 to form1value of form1

F1.form1value = 222;

 

5. Public attribute values of the form and application. openforms attributes

Description: application. openforms attributes: obtains an application.Program. (This attribute is in. Net framework2.0)

The implementation code is as follows:

In form form1

Public int form1value = 1;

Form2 F2 = new form2 ();

F2.show ();

In form form2

String formname = "form1 ";

Form Fr = application. openforms [formname];

If (fr! = NULL)

{

Form1 F1 = (form1) fr;

// The value of form1 is 1.

MessageBox. Show (f1.form1value. tostring ());

// Assign a value of 222 to form1value of form1

F1.form1value = 222;

}

 

6. Events

The implementation code is as follows:

Define the public attribute form2value in the form form2 to get and set the text value of textbox1

It also defines an accept event.

Public String form2value

{

Get

{

Return this. textbox1.text;

}

Set

{

This. textbox1.text = value;

}

}

Public event eventhandler accept;

Private void button#click (Object sender, eventargs E)

{

If (accept! = NULL)

{

Accept (this, eventargs. Empty); // when the form triggers an event, pass its own reference

}

}

In form form1

Form2 F2 = new form2 ();

F2.accept + = new eventhandler (f2_accept );

F2.show ();

Void f2_accept (Object sender, eventargs E)

{

// The Event receiver obtains the reference of form2 through a simple type conversion.

Form2 F2 = (form2) sender;

// Receives the textbox1.text of form2.

This. textbox1.text = f2.form2value;

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.