EventHandler中代码using system;
namespace EventHandler
{
    public delegate int RunHandler(object sender, EventArgs e);
}
Test 中代码using System;
using EventHandler;
namespace Test
{
public class A
{
protected event RunHandler OnBeforeFunctionRun; protected virtual int ExecuteSubFunction(object sender, EventArgs e)
{
return 0;
} }
}
txt 中代码using System;
using Test;
using EventHandler;
namespace txt
{
public class B : A
{
   protected override int ExecuteSubFunction(object sender, EventArgs e)
   {
      if (this.OnBeforeFunctionRun != null)
               {
                 int iRetValue = OnBeforeFunctionRun(sender, e);
                 if (iRetValue != 0)
                 {                   
                   return iRetValue;
                  }
                  else
                 {
                    return -1;
                  }
               }
  }
}

}
最后一段代码如何修改才能不报错?

解决方案 »

  1.   

    public class B : A
        {
           protected override int ExecuteSubFunction(object sender, EventArgs e)
           {
              if (this.OnBeforeFunctionRun != null)
                   {
                     int iRetValue = OnBeforeFunctionRun(sender, e);
                     if (iRetValue != 0)
                     {                   
                       return iRetValue;
                      }
                      else
                     {
                        return -1;
                      }
                   }
          }
        }
        
    }大概看了你的代码。
         protected override int ExecuteSubFunction(object sender, EventArgs e)虽然你继承了。但是(object sender, EventArgs e) 还是会出现问题的。
    用,看一下参数。。
      

  2.   

    private void 。。_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Clicks == 1 && e.Button == MouseButtons.Left)
                {
                    。。
                }
    我的事件是这样调用的~~
      

  3.   

    public class A
    {
        protected event RunHandler OnBeforeFunctionRun;    protected virtual int ExecuteSubFunction(object sender, EventArgs e)
        {
            return 0;
        }    // ========增加方法=======
         protected int RaiseEvent(object sender, EventArgs e)
        {
            if (OnBeforeFunctionRun != null)
            {
                int iRetValue = OnBeforeFunctionRun(sender, e);
                if (iRetValue != 0)
                {
                    return iRetValue;
                }
                else
                {
                    return -1;
                }
            }        return -1;           
        }
        // ======================
    }public class B : A
    {
        protected override int ExecuteSubFunction(object sender, EventArgs e)
        {
            return RaiseEvent(sender, e);
        }
    }
      

  4.   

    c#中要自定义事件,这个对象必须继承EventArgs才能实现
      

  5.   

    1.你事件名称定义有问题,一般以ed或ing结尾。
    2.用protected作为事件的修饰符貌似没有意义
    3.你的错误信息应该贴出来,我看是因为没有返回值的问题,在if外return一个值就行了
      

  6.   

    http://www.cnblogs.com/anjou/archive/2007/06/19/788987.html
    楼主看看是不是要这样
      

  7.   

    另外不声明RunHandler类型的事件OnBeforeFunctionRun,直接声明RunHandler委托的实例可以避免这样的问题.
    test中改成    public class A
        {
            //protected event RunHandler OnBeforeFunctionRun;
            protected RunHandler OnBeforeFunctionRun;
            protected virtual int ExecuteSubFunction(object sender, EventArgs e)
            {
                return 0;
            }    }