C # implement value transfer between windows

Source: Internet
Author: User

C # implement value transfer between windows

This article describes how to use static classes and static variables in C # To transfer values between windows. It is very practical. If you need them, you can refer to it.

To solve the problem of transferring values between multiple windows, we can set static classes and static variables to transfer values between windows.

Form 1 code

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

// Form 1 code

Using System;

Using System. Collections. Generic;

Using System. ComponentModel;

Using System. Data;

Using System. Drawing;

Using System. Linq;

Using System. Text;

Using System. Threading. Tasks;

Using System. Windows. Forms;

 

Namespace WindowsFormsApplication1

{

Public partial class Form1: Form

{

Public Form1 ()

{

InitializeComponent ();

}

 

Private void button#click (object sender, EventArgs e)

{

Sharedclass. sharedvalue = textBox1.Text. ToString (); // usage of static variables: Class Name. variable name assigned to static variables

Form2 frm2 = new Form2 ();

Frm2.Show ();

}

}

Public static class sharedclass // set a static class sharedclass in the namespace. Do not place it before form1.

{

Public static string sharedvalue; // set a static variable sharedvalue.

}

}

Form 2 code

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// Form 2 code

Using System;

Using System. Collections. Generic;

Using System. ComponentModel;

Using System. Data;

Using System. Drawing;

Using System. Linq;

Using System. Text;

Using System. Threading. Tasks;

Using System. Windows. Forms;

 

Namespace WindowsFormsApplication1

{

Public partial class Form2: Form

{

Public Form2 ()

{

InitializeComponent ();

TextBox1.Text = sharedclass. sharedvalue; // input static variables to textBox of window 2

}

}

}

The above is all the content of this article. I hope you will like it.

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.