Value Transfer Mode 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 public static int I = 9 in form1;

The buttons in form2 are as follows:

Private void button#click (Object sender, system. eventargs E)

{

Textbox1.text = form1. I. tostring ();

}

The second method is to use attributes. For details, refer to blog:

Http://blog.csdn.net/tjvictor/archive/2006/06/04/772711.aspx

 

 

14: 296485 people read comments (3) collect reports

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 exampleProgram. 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. 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 delegate

Public Delegate void returnvalue (int I );

Public returnvalue;

The code for the button in form2 is as follows:

Private void button#click (Object sender, system. eventargs E)

{

If (returnvalue! = NULL)

Returnvalue (8 );

}

The buttons in form1 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 ();

}

Click the button of form2 to change the value in the textbox of form1.

In the four methods,

The first one is two-way data transfer. That is to say, if form1 and form2 change the I value, the other party 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.

 

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.