自定义事件未实例化

时间:2014-10-22 07:03:51

标签: c# events

更新 为了简洁而道歉,我迅速打破了这个例子,希望它能够很容易确定。我基本上有一个继承自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。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您的代码snipets太短,无法告诉您如何在您的情况下执行此操作,这是完整的示例。

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 example。如果有什么东西不清楚,你可以阅读,它们是更多的解释。

答案 1 :(得分:1)

你创建了你的事件处理程序,但你从未附加过任何方法(据我所知,从你的代码中可以看出) 请尝试以下方法:

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