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

爱⌒轻易说出口 提交于 2019-11-29 06:18:11

步骤

可以通过构造函数在两个窗体间传递参数。例子由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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!