zoukankan      html  css  js  c++  java
  • C#使用事件

    using System;
    namespace MyCollections
    {
       using System.Collections;

       public delegate void ChangedEventHandler(object sender, EventArgs e);
       public class ListWithChangedEvent: ArrayList
       {
           public event ChangedEventHandler MyAdd;

           public event ChangedEventHandler MyClear;
          protected virtual void OnAdd(EventArgs e)
          {
              if (MyAdd != null)
                  MyAdd(this, e);
          }
           protected virtual void OnClear(EventArgs e)
           {
               if (MyClear != null)
                   MyClear(this, e);
           }

          public override int Add(object value)
          {
             int i = base.Add(value);
             OnAdd(EventArgs.Empty);
             return i;
          }

          public override void Clear()
          {
             base.Clear();
             OnClear(EventArgs.Empty);
          }
       }

       class Test
       {

          // This will be called whenever the list changes.
          public static void AddChanged(object sender, EventArgs e)
          {
             Console.WriteLine("This is called when the event fires.    Add");
          }

           public static void ClearChanged(object sender, EventArgs e)
           {
               Console.WriteLine("This is called when the event fires. Clear");
           }
          public static void Main()
          {
           ListWithChangedEvent list = new ListWithChangedEvent();
           list.MyAdd += new ChangedEventHandler(AddChanged);
           list.MyAdd += new ChangedEventHandler(AddChanged);
           list.MyClear += new ChangedEventHandler(ClearChanged);
           list.Add("item 1");
           list.Clear();
           list.Add("item 1");
           list.Clear();
        
          }
       }
    }

  • 相关阅读:
    CMDB
    Linux文档编辑
    Linux文件管理
    Scrapy框架全
    爬虫基础
    C#中的泛型-1
    网络协议
    PDF抽取文字 C# with Adobe API
    PDF转图片 C# with Adobe API
    浅谈Exchange 2013开发-如何操作邮件的附件
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/1699895.html
Copyright © 2011-2022 走看看