我想执行以下操作,但收到错误参数类型DoWindow
无法分配给参数System.EventHandler
如何让我的委托继承System.EventHandler
?
public delegate void DoWindow(MdiLayout layoutInstruction) ;
private ToolStripMenuItem MakeWindowMenu()
{
var tsi = new ToolStripMenuItem("Window");
tsi.DropDownItems.Add(CreateMenuItem("Cascade","Cascade the features", DoWindowLayout(MdiLayout.Cascade)));
tsi.DropDownItems.Add(CreateMenuItem("Tile Vertical","Tile the features vertically", this.DoWindowTileVertically));
//etc
return tsi;
}
private ToolStripMenuItem CreateMenuItem(string Caption, string tooltip, EventHandler onClickEventHandler)
{
var item = new ToolStripMenuItem(Caption);
item.Click += onClickEventHandler;
item.ToolTipText = tooltip;
return item;
}
public DoWindow DoWindowLayout(MdiLayout layoutInstruction)
{
Master.MDIForm.LayoutMdi(layoutInstruction);
}
答案 0 :(得分:2)
您可以使用Action
作为参数,并使用匿名Eventhandler
来调用Action
类似的东西:
private ToolStripMenuItem MakeWindowMenu()
{
var tsi = new ToolStripMenuItem("Window");
tsi.DropDownItems.Add(CreateMenuItem("Cascade", "Cascade the features", () => Master.MDIForm.LayoutMdi(MdiLayout.Cascade)));
tsi.DropDownItems.Add(CreateMenuItem("Tile Vertical", "Tile the features vertically", () => { }));
return tsi;
}
private ToolStripMenuItem CreateMenuItem(string Caption, string tooltip, Action onClickEventHandler)
{
var item = new ToolStripMenuItem(Caption);
item.Click += (s, e) => { onClickEventHandler.Invoke(); };
item.ToolTipText = tooltip;
return item;
}
答案 1 :(得分:0)
您的自定义EventHandlers需要包含方法所需的签名。换一种说法: ToolStripMenuItemClick需要一个带签名的方法
delegate void EventHandler(object Sender, EventArgs e);
因此您需要更改委托的签名和相应的方法。
有关详细信息,请参阅here。