I have abstract class
public abstract class BaseClass
{
public delegate void ContextMenuClickHandler(object sender, Point point);
public event ContextMenuClickHandler OnContextMenuClick;
public void OnMouseClick(MouseEventArgs e)
{
OnContextMenuClick(this, new Point((int) x, (int) y));
}
}
and I plug event:
Base.ContextMenuClickHa开发者_开发百科ndler += ShowContextMenu;//error
void ShowContextMenu(object sender, Point point)
{
}
error:
'Base.ContextMenuClickHandler' is a 'type', which is not valid in the given context
How to fix it?
The problem here is you are using the event type, ContextMenuClickHandler, instead of the event name, OnContextMenuClick. Try the following
base.OnContextMenuClick += ShowContextMenu
OnContextMenuClick += ShowContextMenu;//fix
you used the wrong variable. OnContextMenuClick
is your event that you subscribe to.
but in your case, why do not use a virtual method?
There is an example here, have a look: http://msdn.microsoft.com/en-us/library/hy3sefw3.aspx
精彩评论