Value transfer between two Windows Forms

Source: Internet
Author: User
Recently, I began to write a program for the winform form, so I searched the data for passing values in the form and found some information, as shown below:
Key words: C # winform value passing
Source: http://blog.csdn.net/lzt7/archive/2006/12/13/1441615.aspx



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. The Code is as follows: Define a static variable public in form1
Static int
I = 9;
The button in form2 is as follows: Private
Void button#click (Object
Sender, system. eventargs E)
{Textbox1.text = form1. I. tostring ();} the second method is to take advantage of properties, please refer to my blog: http://blog.csdn.net/tjvictor/archive/2006/06/04/772711.aspx

This may be an old topic, but today I am using property
But there are new discoveries. That is, if the class is passed, it is passed by address (reference. For example, the following two programs. Assume there are two forms, form1, form2, and
Class1.cs class file. Form1 is the start form of the program. form2 is called through form1. The procedure is as follows:
The content of the class1.cs file is

Public class class1
{
Public int I;
Public class1 ()
{
//
// Todo:
I = 9;
}
Public void modify (int u)
{
I = u;
}
}

The content in form1 is

Private class1 TTT;

Private void form1_load (Object sender, system. eventargs E)
{
TTT = new class1 ();
Form2 temp = new form2 ();
Temp. Change = TTT;
Temp. Show ();
}

Private void button#click (Object sender, system. eventargs E)
{
Textbox1.text = TTT. I. tostring ();
}

Private void button2_click (Object sender, system. eventargs E)
{
TTT. Modify (44 );
}

The content in form2 is:

Private class1 change;
Public class1 change
{
Get {return change ;}
Set
{
Change = value;
}
}

Private void button#click (Object sender, system. eventargs E)
{
Textbox1.text = change. I. tostring ();
}

Private void button2_click (Object sender, system. eventargs E)
{
Change. Modify (98 );
}

Run the program and you will find that, if you change the textbox value in form1
The value will also change accordingly (the color is different, meaning that there is a problem here, because there is no

Where textbox assigns an I value


)
If the I value of change in form2 changes
Also changed accordingly. It's like two forms are using the same data variable. Why?

After thinking, it is actually very simple, because we use the same memory space when using property to transmit data.

When passing data of the class type (as shown above), because we do not have a new instance, but directly assign values, it is equivalent to using a reference and changing the above value assignment process to the following,

Private class1 change;
Public class1 change
{
Get {return change ;}
Set
{
Change = new class1 ();
Change. I = value. I;
}
}

Then the values in two forms are no longer associated with each other. That is to say, changing one of them will not affect the other. This also gives us an inspiration. When we want to pass values between forms and make some connections between values, we can wrap these values with class, re-upload. This is clear and easy to understand.
For example
If the data you pass is not a class but a general data (INT, string
), Then the data is not linked between the form, because C # defines these data types, they are new by default, such as: int I; and INT I = new
INT (); is the same, so when I used to pass simple variables between forms, I did not find the relationship between data until today I passed the class. For example
Class:
Class temp
{
Int I;
Int [] mm;
Public temp ()
{Mm = new int [10];}
}

When passing between forms, variable I is shared by two forms, but mm is not, the reason is what I mentioned above, so using this property feature, we can flexibly pass between forms the values we want to change and do not want to change.

The third method is to use the button of the constructor: form1 to write: Private
Void button#click (Object
Sender, system. eventargs E)
{Form2 temp = new
Form2 (9); temp. Show ();} The form2 constructor is written as follows: public
Form2 (int I)
{Initializecomponent (); textbox1.text = I. tostring ();} The fourth method is to use delegate. The Code is as follows: form2 first defines a delegatepublic
Delegate void
Returnvalue (int
I );
Public
The code of the button in returnvalue; form2 is as follows: Private
Void button#click (Object
Sender, system. eventargs E)
{If
(Returnvalue! = NULL
) Returnvalue (8);} the button in form1 is as follows: Private
Void button#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 button of form2. The value in the textbox in form1 changes accordingly. In the four methods, the first one is two-way value passing, 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 is form1-> form2 unidirectional value transfer. The fourth method is form2-> form1 one-way pass value.

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.