最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#窗體間常用的幾種傳值方式及委托與事件詳解

 更新時間:2019年06月14日 10:54:40   作者:陳彥斌  
這篇文章主要給大家介紹了關于C#窗體間常用的幾種傳值方式及委托與事件的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用小程序具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

窗體間的傳值,最好使用委托方式傳值,開始之前,我們先來說一下委托與事件的關系。

委托:是一個類。

事件:是委托類型的一個特殊實例,只能在類的內部觸發(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)
 {
  //對象內部的,字段或者元素屬性最好不要直接讓外部直接訪問
  //最好是通過,設置的方法來控制一下
  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)內置的委托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)內置的委托注冊事件
   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ù)據類型
   TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg;
   this.SetTxt(arg.Text);
  }
 }
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

相關文章

  • C# 設計模式系列教程-命令模式

    C# 設計模式系列教程-命令模式

    在軟件系統(tǒng)中,行為請求者與行為實現(xiàn)者通常是一種緊耦合的關系,但某些場合,比如需要對行為進行記錄、撤銷或重做、事務等處理時,這種無法抵御變化的緊耦合的設計就不太合適。
    2016-06-06
  • asp.net core 使用 tensorflowjs實現(xiàn) face recognition的源代碼

    asp.net core 使用 tensorflowjs實現(xiàn) face recognition的源代碼

    tensorflowjs,在該項目中使用了ml5js這個封裝過的機器學習JavaScript類庫, 使用起來更簡單,本文給大家分享asp.net core 使用 tensorflowjs實現(xiàn) face recognition的源代碼,需要的朋友參考下吧
    2021-06-06
  • C#異常處理的技巧和方法

    C#異常處理的技巧和方法

    在本篇文章里小編給大家整理了關于C#異常處理的技巧和方法以及相關知識點,需要的朋友們學習下。
    2019-03-03
  • C#基于SerialPort類實現(xiàn)串口通訊詳解

    C#基于SerialPort類實現(xiàn)串口通訊詳解

    這篇文章主要為大家詳細介紹了C#基于SerialPort類實現(xiàn)串口通訊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C#以流方式讀socket超時設置的實例

    C#以流方式讀socket超時設置的實例

    這篇文章主要為大家詳細介紹了C#以流方式讀socket超時設置的實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Silverlight將圖片轉換為byte的實現(xiàn)代碼

    Silverlight將圖片轉換為byte的實現(xiàn)代碼

    這篇文章主要介紹了Silverlight將圖片轉換為byte的實現(xiàn)代碼,需要的朋友可以參考下
    2015-11-11
  • c#中設置快捷鍵

    c#中設置快捷鍵

    c#中設置快捷鍵...
    2007-03-03
  • C#程序員最易犯的編程錯誤

    C#程序員最易犯的編程錯誤

    這篇文章主要介紹了C#程序員最易犯的10個編程錯誤,了解這些錯誤能夠更好地學習C#程序設計,感興趣的小伙伴們可以參考一下
    2015-11-11
  • C#裝箱和拆箱原理詳解

    C#裝箱和拆箱原理詳解

    這篇文章通過圖例主要介紹了C#裝箱和拆箱原理,內容很簡單,感興趣的小伙伴們可以參考一下
    2015-10-10
  • 輕松學習C#的裝箱與拆箱

    輕松學習C#的裝箱與拆箱

    輕松學習C#的裝箱與拆箱,在之前的文章簡單的提到了輕松學習C#的裝箱與拆箱,本文帶著大家更加詳細的介紹輕松學習C#的裝箱與拆箱,感興趣的小伙伴們可以參考一下
    2015-11-11

最新評論

色达县| 皋兰县| 昆山市| 牙克石市| 余姚市| 彭州市| 洪雅县| 眉山市| 衢州市| 鹤山市| 武鸣县| 青岛市| 兰溪市| 天峻县| 河池市| 宣汉县| 金堂县| 四平市| 巴里| 吉木乃县| 合水县| 文昌市| 都昌县| 高碑店市| 西吉县| 修武县| 西青区| 慈利县| 河津市| 开江县| 正定县| 绥宁县| 平乡县| 曲阜市| 固阳县| 广东省| 湟中县| 平原县| 陕西省| 丘北县| 通道|