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.