C#窗體間通訊的幾種常用處理方法總結(jié)
在進(jìn)行C#應(yīng)用程序開發(fā)的過(guò)程中,經(jīng)常需要多窗體之間進(jìn)行數(shù)據(jù)通信,本文舉幾個(gè)例子,把幾種常用的通信方式總結(jié)一下,窗體界面如下圖所示:
主窗體Form1是一個(gè)ListBox,單擊選中某列時(shí),彈出窗體Form2,F(xiàn)orm2中兩個(gè)控件,一個(gè)是TextBox,顯示選中的該列的文本,另一個(gè)是按鈕,點(diǎn)擊時(shí)將修改后的值回傳,且在Form1中修改相應(yīng)的列的文本,同時(shí)Form2關(guān)閉。
方法一:傳值
最先想到的,F(xiàn)orm2構(gòu)造函數(shù)中接收一個(gè)string類型參數(shù),即Form1中選中行的文本,將Form2的TextBox控件的Text設(shè)置為該string,即完成了Form1向Form2的傳值。當(dāng)Form2的AcceptChange按鈕按下,需要修改Form1中ListBox中相應(yīng)列的值,因此可以考慮同時(shí)將Form1中的ListBox控件當(dāng)參數(shù)也傳入Form2,所有修改工作都在Form2中完成,根據(jù)這個(gè)思路,F(xiàn)orm2代碼如下:
publicpartial class Form2 : Form { private string text; private ListBox lb; private int index; //構(gòu)造函數(shù)接收三個(gè)參數(shù):選中行文本,ListBox控件,選中行索引 public Form2(string text,ListBox lb,int index) { this.text = text; this.lb = lb; this.index = index; InitializeComponent(); this.textBox1.Text = text; } private void btnChange_Click(object sender, EventArgs e) { string text = this.textBox1.Text; this.lb.Items.RemoveAt(index); this.lb.Items.Insert(index, text); this.Close(); } }
Form1中new窗體2時(shí)這么寫:
public partial class Form1 :Form { int index = 0; string text = null; public Form1() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgse) { if (this.listBox1.SelectedItem != null) { text = this.listBox1.SelectedItem.ToString(); index = this.listBox1.SelectedIndex; //構(gòu)造Form2同時(shí)傳遞參數(shù) Form2 form2 = new Form2(text, listBox1, index); form2.ShowDialog(); } } }
OK,方法一的解決方法就是這樣,好處是直觀,需要什么就傳什么,缺點(diǎn)也是顯而易見(jiàn)的,如果窗體1中需要修改的是一百個(gè)控件,難道構(gòu)造的時(shí)候還傳100個(gè)參數(shù)進(jìn)去?況且如果其他窗體仍然需要彈Form2,那Form2就廢了,只能供窗體1使用,除非寫重載的構(gòu)造函數(shù),不利于代碼的復(fù)用,繼續(xù)看下一個(gè)方法。
方法二:繼承
這個(gè)方法我試了很多次,繼承的確可以做,但是麻煩不說(shuō),還不方便,因此個(gè)人認(rèn)為如果為了互相操作數(shù)據(jù)而使用繼承,是不合適的,但既然是個(gè)方法,就扔出來(lái)看看,實(shí)際作用≈0。
Form2窗體:
//聲明Form2繼承于Form1 public partial classForm2 : Form1 { publicint index; public ListBox lb; public Form2(string text) { //將繼承過(guò)來(lái)的listBox設(shè)置為不可見(jiàn) this.listBox1.Visible=false; InitializeComponent(); this.textBox1.Text = text; } private void btnChange_Click(object sender, EventArgs e) { string text = this.textBox1.Text; this.lb.Items.RemoveAt(index); this.lb.Items.Insert(index,text); this.Close(); } }
Form1窗體:
public partial class Form1 :Form { public int index = 0; public string text = null; public Form1() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgse) { if (this.listBox1.SelectedItem != null) { text = this.listBox1.SelectedItem.ToString(); index = this.listBox1.SelectedIndex; Form2 form2 = new Form2(text); //構(gòu)造完Form2后,為Form2中各參數(shù)賦值 form2.lb =this.listBox1; form2.index = index; form2.Show(); } } }
這里有幾點(diǎn)問(wèn)題需要注意,F(xiàn)orm2中各屬性需要哪種賦值方法?從Java過(guò)度來(lái)的都知道,Java繼承中在子類中使用關(guān)鍵字super可以訪問(wèn)基類中公有的方法及參數(shù),而C#中super換成了base,那是不是意味著我們可以在Form2中這么為參數(shù)賦值呢?
this.lb=base.listBox1; this.index=base.index;
OK,第二種寫法沒(méi)問(wèn)題,可以保存index值,但是對(duì)ListBox控件,這么賦值就會(huì)出問(wèn)題,通過(guò)測(cè)試我發(fā)現(xiàn),base.listBox1指向的,是子類繼承過(guò)來(lái)的listBox1對(duì)象,并不是基類自己的listBox1對(duì)象。因此我們猜測(cè),那base.index值是不是也是指向子類的index呢?測(cè)試一下發(fā)現(xiàn)的確是這樣,因此this.index=base.index等于沒(méi)寫,去掉照樣可以用,因?yàn)閕ndex一樣被Form2繼承過(guò)來(lái)了,因此我們可以了解到,C#中的窗體繼承,通過(guò)base.控件是無(wú)法操作基類控件的。
方法三:事件回調(diào)
既然C#有事件這個(gè)東西,為啥不用呢,而且事件在窗體通信方面,有著更為方便的作用,我們知道事件實(shí)際上就是狀態(tài)的捕獲,在最后我會(huì)舉一個(gè)捕獲狀態(tài)的例子,先看數(shù)據(jù)互相操作的例子。
Form2窗體:
//定義一個(gè)需要string類型參數(shù)的委托 publicdelegate void MyDelegate(string text); public partial class Form2 :Form1 { //定義該委托的事件 public event MyDelegate MyEvent; public Form2(string text) { InitializeComponent(); this.textBox1.Text = text; } private void btnChange_Click(object sender, EventArgs e) { //觸發(fā)事件,并將修改后的文本回傳 MyEvent(this.textBox1.Text); this.Close(); } }
Form1窗體:
public partial class Form1 :Form { public int index = 0; public string text = null; public Form1() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgse) { if (this.listBox1.SelectedItem != null) { text = this.listBox1.SelectedItem.ToString(); index = this.listBox1.SelectedIndex; Form2 form2 = new Form2(text); //注冊(cè)form2_MyEvent方法的MyEvent事件 form2.MyEvent += new MyDelegate(form2_MyEvent); form2.Show(); } } //處理 void form2_MyEvent(string text) { this.listBox1.Items.RemoveAt(index); this.listBox1.Items.Insert(index, text); } }
可以看出,使用事件確實(shí)是很方便的,并且不需要傳遞那么多參數(shù),不需要有繼承關(guān)系,且提高了代碼重用,因此在一般的需求下,建議這么使用。
相關(guān)文章
c#創(chuàng)建Graphics對(duì)象的三種方法
通常我們使用下述三種方法來(lái)創(chuàng)建一個(gè)Graphics對(duì)象。2013-05-05c# Winform同一數(shù)據(jù)源多個(gè)控件保持同步
通過(guò)對(duì)控件屬性設(shè)置數(shù)據(jù)源綁定,利用Windows數(shù)據(jù)更改通知這一特性,只要訂閱(設(shè)定綁定)的控件都能接收到數(shù)據(jù)的變化通知。 通過(guò)DataBindings方法實(shí)現(xiàn)雙向數(shù)據(jù)綁定2021-06-06C#使用時(shí)序數(shù)據(jù)庫(kù)InfluxDB的教程詳解
InfluxDB是一個(gè)開源的時(shí)序數(shù)據(jù)庫(kù),可以自動(dòng)處理時(shí)間序列數(shù)據(jù),這篇文章主要為大家詳細(xì)介紹了C#如何使用InfluxDB,感興趣的小伙伴可以跟隨小編一起了解下2023-11-11C#實(shí)現(xiàn)常見(jiàn)加密算法的示例代碼
這篇文章主要為大家詳細(xì)介紹一下C#中一些常見(jiàn)加密算法(Base64編碼、凱撒密碼、Vigenere密碼、DES、AES)以及它們的實(shí)現(xiàn)代碼,感興趣的可以了解一下2022-07-07