这个用起来很爽,通常在不知道主窗体是谁,但是要做成 子窗体可以随时获取主窗体的值,如下:
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 窗体传值 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 F = new Form2(); F.ABC += QQQ;//绑定(中介)事件 F.Show(); } int chuanhui = 123; void QQQ(string B, ref int AA) { AA = chuanhui;//返回子窗体的值 } } }
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 窗体传值 { public partial class Form2 : Form { public delegate void D1(string A,ref int B); public event D1 ABC; public Form2() { InitializeComponent(); } int BB; private void button1_Click(object sender, EventArgs e) { ABC("111", ref BB);//执行(中介)事件 } private void button2_Click(object sender, EventArgs e) { MessageBox.Show(BB.ToString());//获取主窗体的值 } } }