自定义事件未实例化

本文关键字:实例化 事件 自定义 | 更新日期: 2023-09-27 18:27:20

更新时间:很抱歉,我很快就把这个例子拼凑起来,希望它能简单地确定。我本质上有一个从DataGridView继承的组件:

public partial class MyGrid: DataGridView
{
    public MyGrid()
    {
        InitializeComponent();
    }
    public delegate void MyEventHandler(object myObject);
    public event MyEventHandler MyEvent;
    public void MyMethod()
    {
        if (MyEvent != null)
        {
            object someObject = "[it varies what I supply here]";
            MyEvent(someObject);
        }
    }
}

在我的表单中,我将组件拖到表单上,并通过MyGrid1的事件窗口连接事件,以便表单的设计器类具有以下条目:

this.MyGrid1.MyEvent += new MyGrid.MyEventHandler(this.MyGrid1_MyEvent);

在形式本身:

private void MyGrid1_MyEvent(object myObject)
{
    //do something....
}

但是MyEvent始终是null,因此该事件从不触发。

我确信我以前从未专门实例化过MyEvent。

有什么想法吗?

自定义事件未实例化

您的代码狙击太短,无法告诉您在您的情况下如何执行,下面是完整的示例。

using System;
namespace MyCollections 
{
   using System.Collections;
   // A delegate type for hooking up change notifications.
   public delegate void ChangedEventHandler(object sender, EventArgs e);
   // 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 ChangedEventHandler 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 ChangedEventHandler(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 ChangedEventHandler(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();
      }
   }
}

这是MSDN的例子。如果你读不清楚的东西,它们更多的是解释。

您创建了事件处理程序,但从未向其附加任何方法(据我从代码中所见)尝试以下操作:

class test{
   Foo myobject = new Foo(); //since you didn't specify your class names
   myobject.MyEvent += myobject.MyEventHandler(method_to_call); 
   public void methodToCall(String s){
        //your logic on event trigger
   }
}