Summary of passing values between Windows Forms

Source: Internet
Author: User
After passing values between Windows Forms, I have summarized four methods: global variables, attributes, form constructors, and delegate. The first global variable: This is the simplest. You only need to describe the variable as static. In form2, you can directly reference the variable form1,CodeAs follows: Define a static variable in form1Public StaticIntI = 9; The buttons in form2 are as follows: Private Void button#click (ObjectSender, system. eventargs E) { Textbox1.text = form1. I. tostring (); }   The second method is to use attributes. For details, see my blog: Http://blog.csdn.net/tjvictor/archive/2006/06/04/772711.aspx   The third method is to use constructor: The button of form1 is written as follows: Private Void button#click (ObjectSender, system. eventargs E) { Form2 temp =NewForm2 (9 ); Temp. Show (); }   The form2 constructor is written as follows: PublicForm2 (Int I) { Initializecomponent (); Textbox1.text = I. tostring (); }   The fourth method is to useThe delegate code is as follows: Form2 first defines a delegate Public DelegateVoidReturnvalue (IntI ); PublicReturnvalue; The code for the button in form2 is as follows: Private Void button#click (ObjectSender, system. eventargs E) { If(Returnvalue! =Null) Returnvalue (8 ); }   The buttons in form1 are as follows: Private Void button#click (ObjectSender, system. eventargs E) { Form2 temp =NewForm2 (); Temp. returnvalue =NewTemp. form2.returnvalue (showvalue ); Temp. Show (); }   Private Void showvalue (IntI) { Textbox1.text = I. tostring (); }   ClickFor button of form2, the value in textbox of form1 changes accordingly.   In the four methods, The first one is bidirectional value transfer, that is,Form1 and form2 change the I value, and the other side will also be affected. The second method can be unidirectional or bidirectional value transfer. The third method isForm1-> form2. The fourth method isForm2-> form1 one-way value transfer.   I will add a new method in the future. Another method is to useEvent, which is similar to delegate, is not mentioned here.
Related Article

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.