窗体间的传值,最好使用委托方式传值,开始以前,咱们先来讲一下委托与事件的关系。安全
首先建立2个窗体,这里咱们以form1为发送窗体,form2为接收窗体this
form1窗体代码spa
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(); } public Form2 msgFrm { get; set; } private void Form1_Load(object sender, EventArgs e) { Form2 f2 = new Form2(); msgFrm = f2; f2.Show(); } private void btnSendMsg_Click(object sender, EventArgs e) { //对象内部的,字段或者元素属性最好不要直接让外部直接访问 //最好是经过,设置的方法来控制一下 msgFrm.SetTxt(this.txtMsg.Text); } } }
form2窗体代码3d
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace 事件的方式实现窗体间传值 12 { 13 public partial class Form2 : Form 14 { 15 public Form2() 16 { 17 InitializeComponent(); 18 } 19 public void SetTxt(string txt) 20 { 21 this.txtMsg.Text = txt; 22 } 23 } 24 }
注:委托不熟悉的宝宝们,请自行查阅Func与Action,以及delegate三者区别,这里咱们用系统内置的委托Actioncode
form1窗体代码orm
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 12 namespace 事件的方式实现窗体间传值 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 //定义委托 21 public Action<string> afterMsgSend { get; set; } 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 Form2 f2 = new Form2(); 25 afterMsgSend += f2.SetTxt; //给系统内置的委托注册事件 26 f2.Show(); 27 } 28 29 private void btnSendMsg_Click(object sender, EventArgs e) 30 { 31 if (afterMsgSend == null) 32 { 33 return; 34 } 35 afterMsgSend(this.txtMsg.Text); 36 } 37 } 38 }
form2窗体代码对象
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace 事件的方式实现窗体间传值 12 { 13 public partial class Form2 : Form 14 { 15 public Form2() 16 { 17 InitializeComponent(); 18 } 19 public void SetTxt(string txt) 20 { 21 this.txtMsg.Text = txt; 22 } 23 } 24 }
TextBoxMsgChangeEventArg类继承EventArgs代码blog
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 事件的方式实现窗体间传值 8 { 9 public class TextBoxMsgChangeEventArg:EventArgs 10 { 11 public string Text { get; set; } 12 } 13 }
form1窗体代码继承
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 12 namespace 事件的方式实现窗体间传值 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 public event EventHandler AfterMsgChange; 21 private void Form1_Load(object sender, EventArgs e) 22 { 23 Form2 f2 = new Form2(); 24 AfterMsgChange += f2.AfterTxtChange; 25 f2.Show(); 26 } 27 private void btnSendMsg_Click(object sender, EventArgs e) 28 { 29 AfterMsgChange(this, new TextBoxMsgChangeEventArg() { Text = this.txtMsg.Text }); 30 } 31 } 32 }
form2窗体事件
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace 事件的方式实现窗体间传值 12 { 13 public partial class Form2 : Form 14 { 15 public Form2() 16 { 17 InitializeComponent(); 18 } 19 public void AfterTxtChange(object sender,EventArgs e) 20 { 21 //拿到父窗体传来的文本,强转数据类型 22 TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg; 23 this.SetTxt(arg.Text); 24 } 25 } 26 }