通过自定义事件来传值。此种方法适合于写驱动程序。进行数据采集。
对于一般的系统事件,是有两个参数的,一个是sender,一个是EventArgs,对于sender,个事件的触发者,一般指向的是一个控件,但是对于EventArgs,一般常用来传递鼠标位置等信息,下面就自定义事件传值就是通过EventArgs来实现。
基础篇
通过EventArgs来实现传值,我们首先需要创建一个类,继承EventArgs,我们可以将需要传递的数据,直接在类里面定义成属性,这里以传递一个布尔(没有再最终的代码内使用)、一个浮点数,一个字符串为例,代码如下所示:
using System;
namespace myEventPrj
{/// <summary>/// 自定义事件/// </summary>public class myEventArgs:EventArgs{public bool BoolValue { get; set; } public float FloatValue { get; set; }public string StrValue { get; set; }}
}
然后我们创建一个类,类里面创建一个事件,并激发事件,这里传递的值为True和12.34,str传输时间,代码如下:
using System;
namespace myEventPrj
{internal class MyTestClass{public void AckEvent(){//触发事件myEvent?.Invoke(this, new myEventArgs(){BoolValue = true,FloatValue = 12.34f,StrValue = DateTime.Now.ToString()});}//创建事件public event EventHandler myEvent;}
}
我们在主窗体中,实例化类,并进行事件绑定,看看能不能获取到传递的数据,代码如下:
using System;
using System.Windows.Forms;namespace myEventPrj
{//https://mp.weixin.qq.com/s?__biz=MzI4OTQyMzY3MQ==&mid=2247493894&idx=1&sn=131e50c046608bf17c09d8118739a2fe&chksm=ed20d4ff08f84521b62e39da3b3ae5533e0fb9ab40852533188fb103cb578bab7010e391b356&mpshare=1&scene=1&srcid=1223Cf4UQDdpgFDmWgaBQgQk&sharer_shareinfo=eba01a08e8f309806a7eb3645b15eb4c&sharer_shareinfo_first=eba01a08e8f309806a7eb3645b15eb4c&exportkey=n_ChQIAhIQGPSzxZb%2BaWifi0gzEAAEfxKfAgIE97dBBAEAAAAAAEVENy%2Bj2rEAAAAOpnltbLcz9gKNyK89dVj0zitidN8l4UPUZ%2FZQP5CCyKDsHp63TUkdE42ttJ%2FabOvWFTHQll95vdd%2FbVBWRQwYVm4Un5z%2FuVQB4ns7YgiHywMP0WaBLonyYRYEiTVYEOmXf3F3YH2rCzkAt4SbP8hsPEYaRO8cFKcX6uEktz6VVUtAQYsp4Z9KPXTkSCGA6S5t9LS50zkSQf5MQU2%2F%2FcKiMgHCeTsvifpWKxy1RY%2Fk8N03%2BqXB6ItdNGjmENQtzHyYdWAcbPKDdg6jmizPSnZCiUP%2FhDYAEOaPSumC0jiLJFS3bTh3EiaIDueE%2F31r71eoDaxKKWrblhVtIqowfWz0ILdBCsAr2VxL&acctmode=0&pass_ticket=CSpjFkwd1sOdAwac3Aa9E25thIeYyMCW8OIM1UwpsNp%2B06JUG23YSoSLKrq1WjGh&wx_header=0#rdpublic partial class Form1 : Form{MyTestClass T;public Form1(){InitializeComponent();T = new MyTestClass();T.myEvent += T_myEvent;}private void T_myEvent(object sender, EventArgs e){myEventArgs arges = e as myEventArgs;richTextBox1.AppendText(arges.StrValue + " " + arges.FloatValue.ToString() + "\r\n");}private void button1_Click(object sender, EventArgs e){T.AckEvent();} }
}
运行结果如下所示:
参考链接
【归】C#在自定义事件里传递自定义数据https://mp.weixin.qq.com/s?__biz=MzI4OTQyMzY3MQ==&mid=2247493894&idx=1&sn=131e50c046608bf17c09d8118739a2fe&chksm=ed20d4ff08f84521b62e39da3b3ae5533e0fb9ab40852533188fb103cb578bab7010e391b356&mpshare=1&scene=1&srcid=1223Cf4UQDdpgFDmWgaBQgQk&sharer_shareinfo=eba01a08e8f309806a7eb3645b15eb4c&sharer_shareinfo_first=eba01a08e8f309806a7eb3645b15eb4c&exportkey=n_ChQIAhIQGPSzxZb%2BaWifi0gzEAAEfxKfAgIE97dBBAEAAAAAAEVENy%2Bj2rEAAAAOpnltbLcz9gKNyK89dVj0zitidN8l4UPUZ%2FZQP5CCyKDsHp63TUkdE42ttJ%2FabOvWFTHQll95vdd%2FbVBWRQwYVm4Un5z%2FuVQB4ns7YgiHywMP0WaBLonyYRYEiTVYEOmXf3F3YH2rCzkAt4SbP8hsPEYaRO8cFKcX6uEktz6VVUtAQYsp4Z9KPXTkSCGA6S5t9LS50zkSQf5MQU2%2F%2FcKiMgHCeTsvifpWKxy1RY%2Fk8N03%2BqXB6ItdNGjmENQtzHyYdWAcbPKDdg6jmizPSnZCiUP%2FhDYAEOaPSumC0jiLJFS3bTh3EiaIDueE%2F31r71eoDaxKKWrblhVtIqowfWz0ILdBCsAr2VxL&acctmode=0&pass_ticket=CSpjFkwd1sOdAwac3Aa9E25thIeYyMCW8OIM1UwpsNp%2B06JUG23YSoSLKrq1WjGh&wx_header=0#rd
代码链接
【免费】C#自定义事件2024年12月23日资源-CSDN文库https://download.csdn.net/download/anlog/90170402
特此记录
anlog
2024年12月23日