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;
}
}
}
}
}
最后一段代码如何修改才能不报错?
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;
}
}
}
}
}
最后一段代码如何修改才能不报错?
{
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) 还是会出现问题的。
用,看一下参数。。
{
if (e.Clicks == 1 && e.Button == MouseButtons.Left)
{
。。
}
我的事件是这样调用的~~
{
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);
}
}
2.用protected作为事件的修饰符貌似没有意义
3.你的错误信息应该贴出来,我看是因为没有返回值的问题,在if外return一个值就行了
楼主看看是不是要这样
test中改成 public class A
{
//protected event RunHandler OnBeforeFunctionRun;
protected RunHandler OnBeforeFunctionRun;
protected virtual int ExecuteSubFunction(object sender, EventArgs e)
{
return 0;
} }