c#如何在程序中定义和使用自定义事件


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

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#如何在程序中定义和使用自定义事件的详细内容!

返回前面的内容

相关阅读 >>

c#如何在程序中定义和使用自定义事件

更多相关阅读请进入《自定义事件》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...