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); } }

      

  • 相关阅读:
    POJ
    Fence Repair POJ
    COGS 2096. 不平凡的许愿树
    COGS 2095. 不平凡的引线
    BZOJ 4392 卡牌游戏
    COGS 2089. 平凡的测试数据
    Slim Span (最小生成树)
    【BZOJ1294】[SCOI2009]围豆豆Bean 射线法+状压DP+SPFA
    排列perm HYSBZ
    I
  • 原文地址:https://www.cnblogs.com/kfx2007/p/2708711.html
Copyright © 2011-2022 走看看