国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#窗體間常用的幾種傳值方式及委托與事件詳解

2020-01-24 00:04:32
字體:
來源:轉載
供稿:網友

前言

窗體間的傳值,最好使用委托方式傳值,開始之前,我們先來說一下委托與事件的關系。

委托:是一個類。

事件:是委托類型的一個特殊實例,只能在類的內部觸發執行。

首先創建2個窗體,這里我們以form1為發送窗體,form2為接收窗體

form1窗體


form2窗體

 

方式一(最簡單的方式)

form1窗體代碼

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窗體代碼

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 Form2()  {   InitializeComponent();  }  public void SetTxt(string txt)  {   this.txtMsg.Text = txt;  } }}

方式二(委托方式)

注:委托不熟悉的寶寶們,請自行查閱Func與Action,以及delegate三者區別,這里我們用系統內置的委托Action

form1窗體代碼

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 Action<string> afterMsgSend { get; set; }  private void Form1_Load(object sender, EventArgs e)  {   Form2 f2 = new Form2();   afterMsgSend += f2.SetTxt; //給系統內置的委托注冊事件   f2.Show();  }  private void btnSendMsg_Click(object sender, EventArgs e)  {   if (afterMsgSend == null)   {    return;   }   afterMsgSend(this.txtMsg.Text);  } }}

form2窗體代碼

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 Form2()  {   InitializeComponent();  }  public void SetTxt(string txt)  {   this.txtMsg.Text = txt;  } }}

方式三(事件方式,更安全喲)

TextBoxMsgChangeEventArg類繼承EventArgs代碼

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 事件的方式實現窗體間傳值{ public class TextBoxMsgChangeEventArg:EventArgs {  public string Text { get; set; } }}

form1窗體代碼

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 event EventHandler AfterMsgChange;  private void Form1_Load(object sender, EventArgs e)  {   Form2 f2 = new Form2();   AfterMsgChange += f2.AfterTxtChange;   f2.Show();  }  private void btnSendMsg_Click(object sender, EventArgs e)  {   AfterMsgChange(this, new TextBoxMsgChangeEventArg() { Text = this.txtMsg.Text });  } }}

form2窗體

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 Form2()  {   InitializeComponent();  }  public void AfterTxtChange(object sender,EventArgs e)  {   //拿到父窗體傳來的文本,強轉數據類型   TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg;   this.SetTxt(arg.Text);  } }}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大安市| 古丈县| 广宁县| 冀州市| 大埔县| 云南省| 宁阳县| 汶上县| 马关县| 施甸县| 冷水江市| 鞍山市| 天门市| 常德市| 嘉黎县| 大英县| 凭祥市| 莱西市| 丘北县| 墨江| 崇左市| 长岭县| 科技| 专栏| 冀州市| 灵台县| 禄丰县| 垣曲县| 长春市| 兴海县| 杂多县| 辛集市| 浦江县| 承德县| 抚顺县| 南宁市| 卓资县| 上高县| 城固县| 新昌县| 岑巩县|