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

posted @ 2019-09-04 13:49  陆陆无为而治者  阅读(7187)  评论(0编辑  收藏  举报