[Read] C # transfer data between windows

Source: Internet
Author: User
Transferred from: http://blog.sina.com.cn/s/blog_62d120530100i6fp.html transfers 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 static int I = 9 in form1; the button in Form2 is as follows: private void button#click (object sender, System. eventArgs e) {textBox1.Text = Form1. I. toString ();

}

Method 2:

This may be an old topic, but today I have made new discoveries when using property. That is, if the class is passed, it is passed by address (reference. For example, the following two programs. Assume there are two form, form1, form2, and a class file of Class1.cs. 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 );
}

When you run the program, you will find that if you change the textbox value in form1, the I value in change in form2 will also change accordingly, and the I value of change in form2 will also change, the I in ttt in form1 also changes 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.
If the data you pass is not a class but a general data (int, string), the data is not correlated between forms, because C # defines the data type, they are new by default, for example: int I; and int I = new int (); are the same, so when I used to pass simple variables between forms, no relationships between data were found until today when class was passed. Class like the following:
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 constructor:

The button of Form1 is written as follows: private void button#click (object sender, System. eventArgs e) {Form2 temp = new Form2 (9); temp. the Show () ;}form2 constructor writes: 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 returnvalue ReturnValue; the Code of the button in form2 is as follows: private void button#click (object sender, System. eve NtArgs e) {if (ReturnValue! = Null) ReturnValue (8);} In Form1, the buttons are 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 ();}

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.