窗體間的傳值,最好使用委託方式傳值,開始以前,咱們先來講一下委託與事件的關係。安全
首先建立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 }