這篇文章主要為大家展示了“C#中窗體間常用的傳值方式有哪些”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“C#中窗體間常用的傳值方式有哪些”這篇文章吧。
前言
窗體間的傳值,最好使用委托方式傳值,開始之前,我們先來說一下委托與事件的關系。
委托:是一個類。
事件:是委托類型的一個特殊實例,只能在類的內(nèi)部觸發(fā)執(zhí)行。
首先創(chuàng)建2個窗體,這里我們以form1為發(fā)送窗體,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 事件的方式實現(xiàn)窗體間傳值 { 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) { //對象內(nèi)部的,字段或者元素屬性最好不要直接讓外部直接訪問 //最好是通過,設置的方法來控制一下 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 事件的方式實現(xiàn)窗體間傳值 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public void SetTxt(string txt) { this.txtMsg.Text = txt; } } }
方式二(委托方式)
注:委托不熟悉的寶寶們,請自行查閱Func與Action,以及delegate三者區(qū)別,這里我們用系統(tǒng)內(nèi)置的委托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 事件的方式實現(xiàn)窗體間傳值 { 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; //給系統(tǒng)內(nèi)置的委托注冊事件 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 事件的方式實現(xiàn)窗體間傳值 { 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 事件的方式實現(xiàn)窗體間傳值 { 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 事件的方式實現(xiàn)窗體間傳值 { 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 事件的方式實現(xiàn)窗體間傳值 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public void AfterTxtChange(object sender,EventArgs e) { //拿到父窗體傳來的文本,強轉數(shù)據(jù)類型 TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg; this.SetTxt(arg.Text); } } }
以上是“C#中窗體間常用的傳值方式有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
文章名稱:C#中窗體間常用的傳值方式有哪些-創(chuàng)新互聯(lián)
文章路徑:http://www.ekvhdxd.cn/article14/isdge.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站營銷、電子商務、網(wǎng)站建設、品牌網(wǎng)站制作、標簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容