c#在程序中定义和使用自定义事件方法总结
作者:laozhang 时间:2022-07-12 01:45:30
C#在程序中定义和使用自定义事件可以分为以下几个步骤:
步骤1:在类中定义事件
using System;
public class TestClass
{
//....
public event EventHandler TestEvent
}
步骤2:定义事件参数
注意:事件参数类TestEventArgs继承自System.EventArgs
using System;
public class TestEventArgs : EventArgs
{
public TestEventArgs() : base() { }
public string Message { get; set; }
}
步骤3:在TestClass 引发事件
public class TestClass
{
// 这个方法引发事件
public void RaiseTestEvent(string message)
{
if (TestEvent == null) return;
TestEvent(this, new TestEventArgs { Message = message });
}
public event EventHandler TestEvent;
}
步骤4:使用事件
class Program
{
static void Main(string[] args)
{
TestClass tc = new TestClass();
// 挂接事件处理方法
tc.TestEvent += Tc_TestEvent;
Console.WriteLine("按任意键引发事件");
Console.ReadKey();
// 引发事件
tc.RaiseTestEvent("通过事件参数传递的字符串");
Console.WriteLine("按任意键退出");
Console.ReadKey();
}
private static void Tc_TestEvent(object sender, EventArgs e)
{
// 将事件参数强制转换为TestEventArgs
TestEventArgs te = (TestEventArgs)e;
// 显示事件参数中的Message
Console.WriteLine(te.Message);
}
}
完整的程序如下
using System;
public class TestClass
{
public void RaiseTestEvent(string message)
{
if (TestEvent == null) return;
TestEvent(this, new TestEventArgs { Message = message });
}
public event EventHandler TestEvent;
}
public class TestEventArgs : EventArgs
{
public TestEventArgs() : base() { }
public string Message { get; set; }
}
class Program
{
static void Main(string[] args)
{
TestClass tc = new TestClass();
tc.TestEvent += Tc_TestEvent;
Console.WriteLine("按任意键引发事件");
Console.ReadKey();
tc.RaiseTestEvent("通过事件参数传递的字符串");
Console.WriteLine("按任意键退出");
Console.ReadKey();
}
private static void Tc_TestEvent(object sender, EventArgs e)
{
TestEventArgs te = (TestEventArgs)e;
Console.WriteLine(te.Message);
}
}
标签:c#,自定义事件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#-WinForm跨线程修改UI界面的示例
2022-01-14 13:25:44
![](https://img.aspxhome.com/file/2023/0/76420_0s.png)
Java 网络爬虫基础知识入门解析
2022-05-16 02:39:06
![](https://img.aspxhome.com/file/2023/3/77473_0s.png)
Spring实战之清除缓存操作示例
2023-05-31 07:46:26
MyBatis-Plus拦截器实现数据权限控制的示例
2022-12-10 05:10:17
Java shiro安全框架使用介绍
2023-07-09 05:24:35
![](https://img.aspxhome.com/file/2023/3/69653_0s.png)
Java实现通讯录管理系统项目
2022-11-17 02:41:03
![](https://img.aspxhome.com/file/2023/3/71903_0s.jpg)
Java的线程与进程以及线程的四种创建方式
2022-12-01 14:11:07
![](https://img.aspxhome.com/file/2023/5/79905_0s.png)
java 实现迷宫回溯算法示例详解
2023-12-14 23:52:26
![](https://img.aspxhome.com/file/2023/2/62982_0s.png)
Java开源工具iText生成PDF简单实例
2022-09-12 15:06:54
SpringBoot中实现分布式的Session共享的详细教程
2023-08-23 18:23:43
![](https://img.aspxhome.com/file/2023/6/58416_0s.jpg)
maven profile实现多环境配置的示例
2023-09-24 12:02:57
![](https://img.aspxhome.com/file/2023/3/64513_0s.png)
C#调用webservice接口的最新方法教程
2022-12-22 05:07:27
![](https://img.aspxhome.com/file/2023/3/83283_0s.png)
java算法题解牛客BM99顺时针旋转矩阵示例
2021-07-08 18:55:18
![](https://img.aspxhome.com/file/2023/3/80953_0s.jpg)
Java ThreadLocal的使用详解
2023-11-29 04:48:43
深入理解 Java、Kotlin、Go 的线程和协程
2022-05-04 12:39:14
![](https://img.aspxhome.com/file/2023/8/70288_0s.jpg)
Spring的@Validation和javax包下的@Valid区别以及自定义校验注解
2021-06-20 04:06:35
Struts2 文件上传进度条的实现实例代码
2023-04-20 11:13:59
![](https://img.aspxhome.com/file/2023/0/72620_0s.png)
详解tryAcquire()、addWaiter()、acquireQueued()
2022-07-30 10:24:05
Java Fluent Mybatis实战之构建项目与代码生成篇下
2023-11-24 10:59:52
![](https://img.aspxhome.com/file/2023/7/59787_0s.png)
Spring4如何自定义@Value功能详解
2021-12-16 06:31:12