温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中窗体间常用的传值方式有哪些

发布时间:2021-08-11 15:17:30 来源:亿速云 阅读:159 作者:小新 栏目:编程语言

这篇文章主要为大家展示了“C#中窗体间常用的传值方式有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C#中窗体间常用的传值方式有哪些”这篇文章吧。

前言

窗体间的传值,最好使用委托方式传值,开始之前,我们先来说一下委托与事件的关系。

委托:是一个类。

事件:是委托类型的一个特殊实例,只能在类的内部触发执行。

首先创建2个窗体,这里我们以form1为发送窗体,form2为接收窗体

form1窗体

C#中窗体间常用的传值方式有哪些

form2窗体

C#中窗体间常用的传值方式有哪些 

方式一(最简单的方式)

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 事件的方式实现窗体间传值 {  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 事件的方式实现窗体间传值 {  public partial class Form2 : Form  {   public Form2()   {    InitializeComponent();   }   public void SetTxt(string txt)   {    this.txtMsg.Text = txt;   }  } }

方式二(委托方式)

注:委托不熟悉的宝宝们,请自行查阅Func与Action,以及delegate三者区别,这里我们用系统内置的委托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 事件的方式实现窗体间传值 {  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; //给系统内置的委托注册事件    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 事件的方式实现窗体间传值 {  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 事件的方式实现窗体间传值 {  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 事件的方式实现窗体间传值 {  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 事件的方式实现窗体间传值 {  public partial class Form2 : Form  {   public Form2()   {    InitializeComponent();   }   public void AfterTxtChange(object sender,EventArgs e)   {    //拿到父窗体传来的文本,强转数据类型    TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg;    this.SetTxt(arg.Text);   }  } }

以上是“C#中窗体间常用的传值方式有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI