C# 2.0学习之--事件2

翻译 2006年06月23日 18:41:00

 

//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

相关文章推荐

深入理解Yii2.0 (2)事件Event

参考地址: http://www.digpage.com/event.html 1、 yii\base\Event 封装了与时间相关的有关数据并提供了一些功能函数作为辅助class Event...

C#的代理和事件2

游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)]

[源码下载] 游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)] 作者:webabcd 介绍 使用 Silverlight 2...

C#中的委托和事件(2)

  • 2009-03-30 15:36
  • 26KB
  • 下载

微信开发学习 OAUTH2.0 搭配事件推送

//网页授权反馈 function authorization($code) { echo $code; $appid="wx65eff0c7cf9cbca3"; $appse...

extJs 2.0学习笔记(事件注册总结篇)

本来,我也没把这档子事放在眼里,因为简单,例如:   Ext.get("elem").on("click",{fn:function(){alert("此元素被单击了!");}};   这样的代码...

c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2

可以实现例如通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;可以自动点击网页上的按钮等功能    1. 加入对Microsoft Internet Controls的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)