利用委托进行窗体传值
Form1.cs代码:
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 frm2 = new Form2(ShowMsg); frm2.Show(); } void ShowMsg(string str) { label1.Text = str; } } }
Form2.cs代码:
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 delegate void DelTest(string str);//声明一个委托 public partial class Form2 : Form { public DelTest _del; public Form2(DelTest del) { this._del = del; InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { _del(textBox1.Text); } } }