Summary of values between Windows Form (form)

Source: Internet
Author: User

Summary of values between Windows Form (form)

In passing values between Windows Forms, I've summed up four methods: Global variables, properties, form constructors, and delegate. The first global variable: This is the simplest, as long as the variable is described as static, you can directly refer to the Form1 variable in Form2, the code is as follows: Define a static variable in Form1 public static int  I= 9; The button buttons in the Form2 are as follows: Private void button1_click (Object sender, System.EventArgs e) {    TextBox1.Text = Form1.i.tostring ();}   The second method is to use attributes, please blog:http://blog.csdn.net/tjvictor/archive/2006/06/04/772711.aspx  The third method is to use the constructor: Form1 button to write: Private void button1_click (Object sender, System.EventArgs e) {    Form2 temp =  new form2 (9);    temp. Show ();} The  form2 constructor reads: Public form2 ( int i) {    InitializeComponent ();    TextBox1.Text = i.ToString ();}   Fourth method is to use delegate, the code is as follows: Form2 first defines a delegatepublic delegate void returnvalue ( int i The code for the button buttons in the;p Ublic returnvalue Returnvalue;form2 is as follows: Private void button1_click (Object sender, System.EventArgs e) {    if  (returnvalue!= null )         returnvalue (8);} The button keys in the  form1 are as follows: Private void button1_click (Object sender, System.EventArgs e) {     Form2 temp = new form2 ();    temp. ReturnValue = new temp. Form2.returnvalue (showvalue);    temp. Show ();}  private void Showvalue ( int i) {    TextBox1.Text = I.ToString ();}   Click the value in the textbox in Form2 Button,form1 to change accordingly.   In these four methods, the first one is bidirectional, that is, Form1 and Form2 change the value of I and the other side is affected. The second method can also pass a value in either direction or bidirectional. The third method is to form1->form2 a one-way pass value. The fourth method is to form2->form1 a one-way pass value.   There's a new way I'll add, and one is to use the event, and delegate, not to mention here. A summary of values between Windows Forms is passed between Windows Form, and I've summed up four methods: Global variables, properties, Form constructors and delegate. The first global variable: This is the simplest, as long as the variable is described as static, you can directly refer to the Form1 variable in Form2, the code is as follows: Define a static variable in Form1 public static int  I= 9; The button buttons in the Form2 are as follows: Private void button1_click (Object sender, System.EventArgs e) {    TextBox1.Text = Form1.i.tostring ();}   The second method is to use attributes, see blog:http://blog.csdn.net/tjvictor/archive/2006/06/04/772711.aspx  The third method is to use the constructor: Form1 button to write: Private void button1_click (Object sender, System.EventArgs e) {    Form2 temp =  new form2 (9);    temp. Show ();} The  form2 constructor reads: Public form2 ( int i) {    InitializeComponent ();    TextBox1.Text = i.ToString ();}   Fourth method is to use delegate, the code is as follows: Form2 first defines a delegatepublic delegate void returnvalue ( int i The code for the button buttons in the;p Ublic returnvalue Returnvalue;form2 is as follows: Private void button1_click (Object sender, System.EventArgs e) {    if  (returnvalue!= null )          returnvalue (8);} The button keys in the  form1 are as follows: Private void button1_click (Object sender, System.EventArgs e) {     Form2 temp = new form2 ();    temp. ReturnValue =&nbsP;new temp. Form2.returnvalue (showvalue);    temp. Show ();}  private void Showvalue ( int i) {    TextBox1.Text = I.ToString ();}   Click the value in the textbox in Form2 Button,form1 to change accordingly.   In these four methods, the first one is bidirectional, that is, Form1 and Form2 change the value of I and the other side is affected. The second method can also pass a value in either direction or bidirectional. The third method is to form1->form2 a one-way pass value. The fourth method is to form2->form1 a one-way pass value.  

Summary of values between Windows Form (form)

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.