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)