C#窗體間常用的幾種傳值方式及委托與事件詳解
前言
窗體間的傳值,最好使用委托方式傳值,開始之前,我們先來說一下委托與事件的關(guān)系。
委托:是一個(gè)類。
事件:是委托類型的一個(gè)特殊實(shí)例,只能在類的內(nèi)部觸發(fā)執(zhí)行。
首先創(chuàng)建2個(gè)窗體,這里我們以form1為發(fā)送窗體,form2為接收窗體
form1窗體

form2窗體
方式一(最簡(jiǎn)單的方式)
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 事件的方式實(shí)現(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)
{
//對(duì)象內(nèi)部的,字段或者元素屬性最好不要直接讓外部直接訪問
//最好是通過,設(shè)置的方法來控制一下
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 事件的方式實(shí)現(xiàn)窗體間傳值
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void SetTxt(string txt)
{
this.txtMsg.Text = txt;
}
}
}
方式二(委托方式)
注:委托不熟悉的寶寶們,請(qǐng)自行查閱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 事件的方式實(shí)現(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)置的委托注冊(cè)事件
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 事件的方式實(shí)現(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 事件的方式實(shí)現(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 事件的方式實(shí)現(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 事件的方式實(shí)現(xiàn)窗體間傳值
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void AfterTxtChange(object sender,EventArgs e)
{
//拿到父窗體傳來的文本,強(qiáng)轉(zhuǎn)數(shù)據(jù)類型
TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg;
this.SetTxt(arg.Text);
}
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
C# 使用Fiddler捕獲本地HttpClient發(fā)出的請(qǐng)求操作
這篇文章主要介紹了C# 使用Fiddler捕獲本地HttpClient發(fā)出的請(qǐng)求操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
詳解C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用
這篇文章主要介紹了C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用,注意區(qū)分一下簡(jiǎn)單工廠模式、工廠方法模式和抽象工廠模式概念之間的區(qū)別,需要的朋友可以參考下2016-02-02
基于C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單的FTP操作工具
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單的FTP操作工具,可以實(shí)現(xiàn)FTP上傳、下載、重命名、刷新、刪除功能,感興趣的可以了解一下2022-08-08
c#實(shí)現(xiàn)服務(wù)器性能監(jiān)控并發(fā)送郵件保存日志
這篇文章主要介紹了c#實(shí)現(xiàn)服務(wù)器性能監(jiān)控并發(fā)送郵件保存日志的示例,代碼分為客戶端和服務(wù)端,客戶端可安裝為本地服務(wù)形式啟動(dòng)2014-01-01
C#解決SQlite并發(fā)異常問題的方法(使用讀寫鎖)
這篇文章主要介紹了C#解決SQlite并發(fā)異常問題的方法,通過使用讀寫鎖達(dá)到多線程安全訪問,進(jìn)而解決SQLite并發(fā)異常的問題,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
詳解C#把DataTable中數(shù)據(jù)一次插入數(shù)據(jù)庫的方法
本篇文章主要介紹了詳解C#把DataTable中數(shù)據(jù)一次插入數(shù)據(jù)庫的方法,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01

