C# WinForm两个窗体之间传递参数


步骤

  可以通过构造函数在两个窗体间传递参数。例子由Form1传递到Form2(Form1和Form2对应控件略)。

  Form1代码:

 1 //……
 2 public partial class Form1 : Form
 3     {
 4         public Form1()
 5         {
 6             InitializeComponent();
 7         }
 8 
 9         private void button1_Click(object sender, EventArgs e)
10         {
11             Hide();
12             Form2 form = new Form2(textBox1.Text);
13             form.ShowDialog();
14             Show();
15         }
16     }
17 //……

  Form2代码:

 1 //……
 2 string text ;     //用于获取传递的参数
 3         public Form2()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         public Form2(string text)
 9         {
10             InitializeComponent();
11             this.text = text;           
12         }
13 
14         private void Form2_Load(object sender, EventArgs e)
15         {            
16             label1.Text = "Hello, world!" + text;
17         }
18 //……

参考网址

  [1] https://wenku.baidu.com/view/0ab6226687c24028915fc395.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM