zoukankan      html  css  js  c++  java
  • C#窗体间常用的几种传值方式、以及委托与事件的详细介绍

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

      委托:是一个

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

    首先创建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窗体代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 
    11 namespace 事件的方式实现窗体间传值
    12 {
    13     public partial class Form2 : Form
    14     {
    15         public Form2()
    16         {
    17             InitializeComponent();
    18         }
    19         public void SetTxt(string txt)
    20         {
    21             this.txtMsg.Text = txt;
    22         }
    23     }
    24 }

    方式二(委托方式

    注:委托不熟悉的宝宝们,请自行查阅Func与Action,以及delegate三者区别,这里我们用系统内置委托Action

    form1窗体代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 
    11 
    12 namespace 事件的方式实现窗体间传值
    13 {
    14     public partial class Form1 : Form
    15     {
    16         public Form1()
    17         {
    18             InitializeComponent();
    19         }
    20         //定义委托
    21         public Action<string> afterMsgSend { get; set; }
    22         private void Form1_Load(object sender, EventArgs e)
    23         {
    24             Form2 f2 = new Form2();
    25             afterMsgSend += f2.SetTxt; //给系统内置的委托注册事件
    26             f2.Show();
    27         }
    28 
    29         private void btnSendMsg_Click(object sender, EventArgs e)
    30         {
    31             if (afterMsgSend == null)
    32             {
    33                 return;
    34             }
    35             afterMsgSend(this.txtMsg.Text);
    36         }
    37     }
    38 }

    form2窗体代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 
    11 namespace 事件的方式实现窗体间传值
    12 {
    13     public partial class Form2 : Form
    14     {
    15         public Form2()
    16         {
    17             InitializeComponent();
    18         }
    19         public void SetTxt(string txt)
    20         {
    21             this.txtMsg.Text = txt;
    22         }
    23     }
    24 }

    方式三(事件方式,更安全哟

    TextBoxMsgChangeEventArg类继承EventArgs代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 事件的方式实现窗体间传值
     8 {
     9     public class TextBoxMsgChangeEventArg:EventArgs
    10     {
    11         public string Text { get; set; }
    12     }
    13 }

    form1窗体代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 
    11 
    12 namespace 事件的方式实现窗体间传值
    13 {
    14     public partial class Form1 : Form
    15     {
    16         public Form1()
    17         {
    18             InitializeComponent();
    19         }
    20         public event EventHandler AfterMsgChange;
    21         private void Form1_Load(object sender, EventArgs e)
    22         {
    23             Form2 f2 = new Form2();
    24             AfterMsgChange += f2.AfterTxtChange;
    25             f2.Show();
    26         }
    27         private void btnSendMsg_Click(object sender, EventArgs e)
    28         {
    29             AfterMsgChange(this, new TextBoxMsgChangeEventArg() { Text = this.txtMsg.Text });
    30         }
    31     }
    32 }

    form2窗体

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 
    11 namespace 事件的方式实现窗体间传值
    12 {
    13     public partial class Form2 : Form
    14     {
    15         public Form2()
    16         {
    17             InitializeComponent();
    18         }
    19         public void AfterTxtChange(object sender,EventArgs e)
    20         {
    21             //拿到父窗体传来的文本,强转数据类型
    22             TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg;
    23             this.SetTxt(arg.Text);
    24         }
    25     }
    26 }
    还有不懂的宝宝们,欢迎下方留言~
  • 相关阅读:
    iOS 友盟统计
    iOS 高德地图 根据经纬度解析成位置
    十六、Swift 可选值链条 Optional Chaining
    十五、自动引用计数 Automatic Reference Counting
    十四、析构器 Deinitialization
    BCB应对WIN7的放大屏幕文本技术
    十三、初始化 Initialization
    十二、继承 Inheritance
    十一、下标 Subscripts
    十、方法 Methods
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/11019862.html
Copyright © 2011-2022 走看看