方法1:通过Form类构造方法的重载传参。
Form1类中代码:
using System;
2 using System.Windows.Forms;
3
4 namespace WindowsForms跨窗体传值大全
5 {
6 public partial class Form1 : Form
7 {
8 public Form1()
9 {
10 InitializeComponent();
11 }
12
13 private void button1_Click(object sender, EventArgs e)
14 {
15 Form2 f2 = new Form2(textBox1.Text);
16 f2.ShowDialog();
17 }
18 }
19 }
Form2类中代码:
using System;
2 using System.Windows.Forms;
3
4 namespace WindowsForms跨窗体传值大全
5 {
6 public partial class Form2 : Form
7 {
8 public Form2()
9 {
10 InitializeComponent();
11 }
12
13 public Form2(string str)
14 {
15 InitializeComponent();
16 textBox2.Text = str;//这句必须放在InitializeComponent();的后面,否则会引起“空引用异常”
17 }
18
19