zoukankan      html  css  js  c++  java
  • C# 中自定义事件 Kevin

    自定义事件的步骤:

    1.声明委托:

    public delegate yourActionEventHandler(Object sender,ArguEvent e);

    2.声明事件

    public event yourActionEventHandler yourAction;

    3.注册事件:

    Class class = new Class();

    class.yourAction += new yourActionEventHandler(Object sender,ArguEvent e);

    4.实现事件处理函数:

    public void yourActionEventHandler(Object sender,ArguEvent e)

    {

    //Here is your code

    }

    5.触发事件

    public void Button1_Click(Object sender,ArguEvent e)

    {

        yourAction(this,ArguEvent.Empty);

    }

    实例代码, 在class2中声明、触发事件,在class1中注册,处理事件

    class1
        {
            static void Main(string[] args)
            {
                class2 ts = new class2();
                ts.CloseAction += new    class2.CloseActionEventHandler(ts_CloseAction);
    
    
                class2 ts2 = new class2();//此处引入第二个Class2的实例 作对比之用
                ts.Run();
           //ts2.Run(); 该方法如果执行了,编译器会报Class2中的CloseAction未将对象的引用设置到对象的实例,原因就是ts2并没有注册事件。 Console.Read(); } static void ts_CloseAction(object sender, EventArgs e) { Console.WriteLine("触发了Test事件"); } } class2 { public delegate void CloseActionEventHandler(Object sender, EventArgs e); public event CloseActionEventHandler CloseAction; public void Run() { CloseAction(this, EventArgs.Empty); } }

      

  • 相关阅读:
    linux 进程
    VFS dup ,dup2
    文件操作 之 各类函数
    文件系统之 stat与access
    xml文件
    Java学习笔记42(数据库连接池 druid连接池)
    java学习笔记41(数据库连接池 C3p0连接池)
    java学习笔记39(sql事物)
    java学习笔记38(sql注入攻击及解决方法)
    java学习笔记37(sql工具类:JDBCUtils)
  • 原文地址:https://www.cnblogs.com/kfx2007/p/2708711.html
Copyright © 2011-2022 走看看