zoukankan      html  css  js  c++  java
  • C# 2.0学习之事件2

    //Copyright (C) Microsoft Corporation.  All rights reserved.

    // events2.cs
    using System;
    namespace MyCollections
    {
       using System.Collections;

       // A class that works just like ArrayList, but sends event
       // notifications whenever the list changes:
       public class ListWithChangedEvent: ArrayList
       {
          // An event that clients can use to be notified whenever the
          // elements of the list change:
          public event EventHandler Changed;

          // Invoke the Changed event; called whenever list changes:
          protected virtual void OnChanged(EventArgs e)
          {
             if (Changed != null)
                Changed(this,e);
          }

          // Override some of the methods that can change the list;
          // invoke event after each:
          public override int Add(object value)
          {
             int i = base.Add(value);
             OnChanged(EventArgs.Empty);
             return i;
          }

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

          public override object this[int index]
          {
             set
             {
                base[index] = value;
                OnChanged(EventArgs.Empty);
             }
          }
       }
    }

    namespace TestEvents
    {
       using MyCollections;

       class EventListener
       {
          private ListWithChangedEvent List;

          public EventListener(ListWithChangedEvent list)
          {
             List = list;
             // Add "ListChanged" to the Changed event on "List":
             List.Changed += new EventHandler(ListChanged);
          }

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

          public void Detach()
          {
             // Detach the event and delete the list:
             List.Changed -= new EventHandler(ListChanged);
             List = null;
          }
       }

       class Test
       {
          // Test the ListWithChangedEvent class:
          public static void Main()
          {
          // Create a new list:
          ListWithChangedEvent list = new ListWithChangedEvent();

          // Create a class that listens to the list's change event:
          EventListener listener = new EventListener(list);

          // Add and remove items from the list:
          list.Add("item 1");
          list.Clear();
          listener.Detach();
          }
       }
    }


    csc events2.cs
    events2
  • 相关阅读:
    TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)
    分布式与集群的区别是什么?
    模板函数函数模板 Function Template(C++Primer10)
    注意地方hadoop中的pi值计算
    文件错误关于hibernate中报Duplicate class/entity mapping org.model.User错的问题
    元素序列几个常用排序算法:一
    行语句mysql insert操作详解
    分量入度hdu 3836 Equivalent Sets
    查询语句编写高效SQL语句
    方法元素c语言范式编程之lsearch
  • 原文地址:https://www.cnblogs.com/llbofchina/p/434184.html
Copyright © 2011-2022 走看看