偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

總結(jié)幾種C#窗體間通訊的處理方法

開發(fā) 后端
本文介紹了C#窗體間通訊的幾種處理方法,即傳值、繼承、事件回調(diào),希望對(duì)大家有用。

應(yīng)用程序開發(fā)中,經(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)閉。

C#窗體間通訊方法一:傳值

最先想到的,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代碼如下:

  1. publicpartial class Form2 : Form     
  2.     {     
  3.         private string text;     
  4.         private ListBox lb;     
  5.         private int index;     
  6.     
  7.        //構(gòu)造函數(shù)接收三個(gè)參數(shù):選中行文本,ListBox控件,選中行索引     
  8.         public Form2(string text,ListBox lb,int index)     
  9.         {     
  10.             this.text = text;     
  11.             this.lb = lb;     
  12.             this.index = index;     
  13.             InitializeComponent();     
  14.             this.textBox1.Text = text;     
  15.         }     
  16.     
  17.         private void btnChange_Click(object sender, EventArgs e)     
  18.         {                
  19.     
  20.             string text = this.textBox1.Text;     
  21.             this.lb.Items.RemoveAt(index);     
  22.             this.lb.Items.Insert(index, text);     
  23.             this.Close();     
  24.         }     
  25.     }   

Form1中new窗體2時(shí)這么寫:

  1. public partial class Form1 :Form     
  2.     {     
  3.         int index = 0;     
  4.         string text = null;     
  5.         public Form1()     
  6.         {     
  7.             InitializeComponent();     
  8.         }     
  9.     
  10.         private void listBox1_SelectedIndexChanged(object sender, EventArgse)     
  11.         {     
  12.             if (this.listBox1.SelectedItem != null)     
  13.             {     
  14.                 text = this.listBox1.SelectedItem.ToString();     
  15.                 index = this.listBox1.SelectedIndex;     
  16.     
  17.                //構(gòu)造Form2同時(shí)傳遞參數(shù)     
  18.                 Form2 form2 = new Form2(text, listBox1, index);     
  19.                 form2.ShowDialog();     
  20.             }     
  21.         }   

OK,方法一的解決方法就是這樣,好處是直觀,需要什么就傳什么,缺點(diǎn)也是顯而易見的,如果窗體1中需要修改的是一百個(gè)控件,難道構(gòu)造的時(shí)候還傳100個(gè)參數(shù)進(jìn)去?況且如果其他窗體仍然需要彈Form2,那Form2就廢了,只能供窗體1使用,除非寫重載的構(gòu)造函數(shù),不利于代碼的復(fù)用,繼續(xù)看下一個(gè)方法。

C#窗體間通訊方法二:繼承

這個(gè)方法我試了很多次,繼承的確可以做,但是麻煩不說,還不方便,因此個(gè)人認(rèn)為如果為了互相操作數(shù)據(jù)而使用繼承,是不合適的,但既然是個(gè)方法,就扔出來(lái)看看,實(shí)際作用≈0。

Form2: 

  1. //聲明Form2繼承于Form1     
  2.     
  3. public partial classForm2 : Form1     
  4.     {     
  5.          publicint index;     
  6.     
  7.         public ListBox lb;     
  8.         public Form2(string text)     
  9.         {       
  10.     
  11.            //將繼承過來(lái)的listBox設(shè)置為不可見     
  12.     
  13.             this.listBox1.Visible=false;     
  14.             InitializeComponent();     
  15.             this.textBox1.Text = text;     
  16.         }     
  17.         private void btnChange_Click(object sender, EventArgs e)     
  18.         {     
  19.             string text = this.textBox1.Text;     
  20.             this.lb.Items.RemoveAt(index);     
  21.             this.lb.Items.Insert(index,text);              
  22.             this.Close();     
  23.     
  24.         }     
  25.     }   

Form1:

  1. public partial class Form1 :Form     
  2.     {     
  3.         public int index = 0;     
  4.         public string text = null;     
  5.         public Form1()     
  6.         {     
  7.             InitializeComponent();     
  8.         }     
  9.     
  10.         private void listBox1_SelectedIndexChanged(object sender, EventArgse)     
  11.         {     
  12.             if (this.listBox1.SelectedItem != null)     
  13.             {     
  14.                 text = this.listBox1.SelectedItem.ToString();     
  15.                 index = this.listBox1.SelectedIndex;     
  16.                 Form2 form2 = new Form2(text);     
  17.     
  18.                //構(gòu)造完Form2后,為Form2中各參數(shù)賦值     
  19.                 form2.lb =this.listBox1;                    
  20.                 form2.index = index;     
  21.                 form2.Show();     
  22.             }     
  23.         }     
  24.     }   

這里有幾點(diǎn)問題需要注意,F(xiàn)orm2中各屬性需要哪種賦值方法?從Java過度來(lái)的都知道,Java繼承中在子類中使用關(guān)鍵字super可以訪問基類中公有的方法及參數(shù),而C#中super換成了base,那是不是意味著我們可以在Form2中這么為參數(shù)賦值呢?

  1. this.lb=base.listBox1;     
  2.     
  3. this.index=base.index;    

OK,第二種寫法沒問題,可以保存index值,但是對(duì)ListBox控件,這么賦值就會(huì)出問題,通過測(cè)試我發(fā)現(xiàn),base.listBox1指向的,是子類繼承過來(lái)的listBox1對(duì)象,并不是基類自己的listBox1對(duì)象。因此我們猜測(cè),那base.index值是不是也是指向子類的index呢?測(cè)試一下發(fā)現(xiàn)的確是這樣,因此this.index=base.index等于沒寫,去掉照樣可以用,因?yàn)閕ndex一樣被Form2繼承過來(lái)了,因此我們可以了解到,C#中的窗體繼承,通過base.控件是無(wú)法操作基類控件的。

C#窗體間通訊方法三:事件回調(diào)

既然C#有事件這個(gè)東西,為啥不用呢,而且事件在窗體通信方面,有著更為方便的作用,我們知道事件實(shí)際上就是狀態(tài)的捕獲,在最后我會(huì)舉一個(gè)捕獲狀態(tài)的例子,先看數(shù)據(jù)互相操作的例子。

Form2:

  1. //定義一個(gè)需要string類型參數(shù)的委托     
  2.     
  3. publicdelegate void MyDelegate(string text);     
  4.     
  5. public partial class Form2 :Form1     
  6.     {     
  7.     
  8.        //定義該委托的事件     
  9.         public event MyDelegate MyEvent;     
  10.         public Form2(string text)     
  11.         {      
  12.             InitializeComponent();     
  13.             this.textBox1.Text = text;     
  14.         }     
  15.         private void btnChange_Click(object sender, EventArgs e)     
  16.         {     
  17.     
  18.            //觸發(fā)事件,并將修改后的文本回傳     
  19.             MyEvent(this.textBox1.Text);     
  20.             this.Close();     
  21.         }     
  22.     }   

Form1: 

  1. public partial class Form1 :Form     
  2.     {     
  3.         public int index = 0;     
  4.         public string text = null;     
  5.         public Form1()     
  6.         {     
  7.             InitializeComponent();     
  8.         }     
  9.     
  10.         private void listBox1_SelectedIndexChanged(object sender, EventArgse)     
  11.         {     
  12.             if (this.listBox1.SelectedItem != null)     
  13.             {     
  14.                 text = this.listBox1.SelectedItem.ToString();     
  15.                 index = this.listBox1.SelectedIndex;     
  16.                 Form2 form2 = new Form2(text);     
  17.     
  18.                //注冊(cè)form2_MyEvent方法的MyEvent事件     
  19.                 form2.MyEvent += new MyDelegate(form2_MyEvent);     
  20.                 form2.Show();     
  21.             }     
  22.         }     
  23.     
  24.        //處理     
  25.     
  26.         void form2_MyEvent(string text)     
  27.         {     
  28.             this.listBox1.Items.RemoveAt(index);     
  29.             this.listBox1.Items.Insert(index, text);     
  30.         }     
  31.     }   

可以看出,使用事件做是很方便的,并且不需要傳遞那么多參數(shù),不需要有繼承關(guān)系,且提高了代碼重用,因此在一般的需求下,建議這么使用。

【編輯推薦】

  1. 解密C#-SQLite是如何移植的
  2. 看看如何透過JavaScript調(diào)用C#函數(shù)
  3. 淺析C#事件注冊(cè)和注銷
  4. 示例:C#通過AMO對(duì)象瀏覽SQL SERVER 2005 SSAS
  5. C#隱藏窗口的幾種方法
責(zé)任編輯:book05 來(lái)源: coolszy
相關(guān)推薦

2009-09-07 03:44:50

C#窗體間傳值

2009-07-30 16:40:03

C#日期格式化

2010-08-31 09:46:23

C#

2009-08-19 15:54:33

處理C#消息

2009-09-14 18:11:23

C#排序方法

2009-07-30 15:57:30

C#時(shí)間

2009-08-31 09:19:31

c#隱藏窗口

2010-06-13 09:15:16

WinForm窗體

2013-02-25 10:18:08

ThreadMsgC#

2024-07-12 08:22:05

C#SendMessag技術(shù)

2009-09-09 13:31:15

C# TextBox

2013-02-22 09:54:15

C#Excel讀取Excel

2009-08-06 17:24:08

C#字符串

2009-08-10 14:23:39

C# Setting

2009-08-20 10:10:55

C#透明窗體

2009-08-03 11:37:36

C#日期時(shí)間控件

2009-09-01 18:35:53

C#判斷文件存在

2009-09-24 14:59:38

C#編寫COM組件

2009-08-21 18:01:32

C#匿名方法

2009-08-12 17:32:44

C#反射方法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)