C#窗体间常用的几种传值方式及委托与事件详解

所属分类: 软件编程 / C#教程 阅读数: 114
收藏 0 赞 0 分享

前言

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

委托:是一个类。

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

首先创建2个窗体,这里我们以form1为发送窗体,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 事件的方式实现窗体间传值
{
 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#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多