C#窗體間常用的幾種傳值方式、以及委托與事件的詳細(xì)介紹

窗體間的傳值勇吊,最好使用委托方式傳值曼追,開(kāi)始之前,我們先來(lái)說(shuō)一下委托與事件的關(guān)系萧福。

委托:是一個(gè)?類?拉鹃。

事件:是?委托類型?的一個(gè)?特殊實(shí)例?,只能在?類?的?內(nèi)部觸發(fā)執(zhí)行?鲫忍。

首先創(chuàng)建2個(gè)窗體膏燕,這里我們以form1為發(fā)送窗體,form2為接收窗體

form1窗體

form2窗體

方式一(?最簡(jiǎn)單的方式?)

form1窗體代碼

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace事件的方式實(shí)現(xiàn)窗體間傳值{publicpartialclassForm1 : Form? ? {publicForm1(){? ? ? ? ? ? InitializeComponent();? ? ? ? }publicForm2 msgFrm { get;set; }privatevoidForm1_Load(object sender, EventArgs e){? ? ? ? ? ? Form2 f2 =newForm2();? ? ? ? ? ? msgFrm = f2;? ? ? ? ? ? f2.Show();? ? ? ? }privatevoidbtnSendMsg_Click(object sender, EventArgs e){//對(duì)象內(nèi)部的悟民,字段或者元素屬性最好不要直接讓外部直接訪問(wèn)//最好是通過(guò)坝辫,設(shè)置的方法來(lái)控制一下msgFrm.SetTxt(this.txtMsg.Text);? ? ? ? ? ? ? ? ? ? }? ? }}

form2窗體代碼

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Linq;7usingSystem.Text;8usingSystem.Threading.Tasks;9usingSystem.Windows.Forms;1011namespace事件的方式實(shí)現(xiàn)窗體間傳值12{13publicpartialclassForm2 : Form14{15publicForm2()16{17InitializeComponent();18}19publicvoidSetTxt(stringtxt)20{21this.txtMsg.Text = txt;22}23}24}

方式二(?委托方式?)

注:委托不熟悉的寶寶們,請(qǐng)自行查閱Func與Action射亏,以及delegate三者區(qū)別近忙,這里我們用系統(tǒng)?內(nèi)置?的?委托Action

form1窗體代碼

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Linq;7usingSystem.Text;8usingSystem.Threading.Tasks;9usingSystem.Windows.Forms;101112namespace事件的方式實(shí)現(xiàn)窗體間傳值13{14publicpartialclassForm1 : Form15{16publicForm1()17{18InitializeComponent();19}20//定義委托21publicAction afterMsgSend { get;set; }22privatevoidForm1_Load(object sender, EventArgs e)23{24Form2 f2 =newForm2();25afterMsgSend += f2.SetTxt;//給系統(tǒng)內(nèi)置的委托注冊(cè)事件26f2.Show();27}2829privatevoidbtnSendMsg_Click(object sender, EventArgs e)30{31if(afterMsgSend == null)32{33return;34}35afterMsgSend(this.txtMsg.Text);36}37}38}

form2窗體代碼

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Linq;7usingSystem.Text;8usingSystem.Threading.Tasks;9usingSystem.Windows.Forms;1011namespace事件的方式實(shí)現(xiàn)窗體間傳值12{13publicpartialclassForm2 : Form14{15publicForm2()16{17InitializeComponent();18}19publicvoidSetTxt(stringtxt)20{21this.txtMsg.Text = txt;22}23}24}

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

TextBoxMsgChangeEventArg?類繼承?EventArgs?代碼

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;5usingSystem.Threading.Tasks;67namespace事件的方式實(shí)現(xiàn)窗體間傳值8{9publicclassTextBoxMsgChangeEventArg:EventArgs10{11publicstringText { get;set; }12}13}

form1窗體代碼

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Linq;7usingSystem.Text;8usingSystem.Threading.Tasks;9usingSystem.Windows.Forms;101112namespace事件的方式實(shí)現(xiàn)窗體間傳值13{14publicpartialclassForm1 : Form15{16publicForm1()17{18InitializeComponent();19}20publicevent EventHandler AfterMsgChange;21privatevoidForm1_Load(object sender, EventArgs e)22{23Form2 f2 =newForm2();24AfterMsgChange += f2.AfterTxtChange;25f2.Show();26}27privatevoidbtnSendMsg_Click(object sender, EventArgs e)28{29AfterMsgChange(this,newTextBoxMsgChangeEventArg() { Text =this.txtMsg.Text });30}31}32}

form2窗體

1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Linq;7usingSystem.Text;8usingSystem.Threading.Tasks;9usingSystem.Windows.Forms;1011namespace事件的方式實(shí)現(xiàn)窗體間傳值12{13publicpartialclassForm2 : Form14{15publicForm2()16{17InitializeComponent();18}19publicvoidAfterTxtChange(object sender,EventArgs e)20{21//拿到父窗體傳來(lái)的文本智润,強(qiáng)轉(zhuǎn)數(shù)據(jù)類型22TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg;23this.SetTxt(arg.Text);24}25}26}

看我主頁(yè)簡(jiǎn)介免費(fèi)C++學(xué)習(xí)資源及舍,視頻教程、職業(yè)規(guī)劃窟绷、面試詳解锯玛、學(xué)習(xí)路線、開(kāi)發(fā)工具

每晚8點(diǎn)直播講解C++編程技術(shù)。非常感謝大家的關(guān)注

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末攘残,一起剝皮案震驚了整個(gè)濱河市拙友,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌歼郭,老刑警劉巖遗契,帶你破解...
    沈念sama閱讀 219,110評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異病曾,居然都是意外死亡牍蜂,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門知态,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)捷兰,“玉大人,你說(shuō)我怎么就攤上這事负敏」泵” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 165,474評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵其做,是天一觀的道長(zhǎng)顶考。 經(jīng)常有香客問(wèn)我,道長(zhǎng)妖泄,這世上最難降的妖魔是什么驹沿? 我笑而不...
    開(kāi)封第一講書人閱讀 58,881評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮蹈胡,結(jié)果婚禮上渊季,老公的妹妹穿的比我還像新娘。我一直安慰自己罚渐,他們只是感情好却汉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著荷并,像睡著了一般合砂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上源织,一...
    開(kāi)封第一講書人閱讀 51,698評(píng)論 1 305
  • 那天翩伪,我揣著相機(jī)與錄音,去河邊找鬼谈息。 笑死缘屹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的侠仇。 我是一名探鬼主播囊颅,決...
    沈念sama閱讀 40,418評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了踢代?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,332評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤嗅骄,失蹤者是張志新(化名)和其女友劉穎胳挎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體溺森,經(jīng)...
    沈念sama閱讀 45,796評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡慕爬,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了屏积。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片医窿。...
    茶點(diǎn)故事閱讀 40,110評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖炊林,靈堂內(nèi)的尸體忽然破棺而出姥卢,到底是詐尸還是另有隱情,我是刑警寧澤渣聚,帶...
    沈念sama閱讀 35,792評(píng)論 5 346
  • 正文 年R本政府宣布独榴,位于F島的核電站,受9級(jí)特大地震影響奕枝,放射性物質(zhì)發(fā)生泄漏棺榔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評(píng)論 3 331
  • 文/蒙蒙 一隘道、第九天 我趴在偏房一處隱蔽的房頂上張望症歇。 院中可真熱鬧,春花似錦谭梗、人聲如沸忘晤。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,003評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)德频。三九已至,卻和暖如春缩幸,著一層夾襖步出監(jiān)牢的瞬間壹置,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,130評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工表谊, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留钞护,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,348評(píng)論 3 373
  • 正文 我出身青樓爆办,卻偏偏與公主長(zhǎng)得像难咕,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容