开发者

how do I get the index of a firing context menu item

I would like to determine the index of the selected context menu item. But I don't want to build a unique eventhandler for each menu item...how do I do this

MenuItem开发者_高级运维 mi=   StatusContextMenu.MenuItems.Add("Set option");
  mi.MenuItems.Add(0,new MenuItem("Set to A", new EventHandler(SetLetter)) );
  mi.MenuItems.Add(1,new MenuItem("Set to B", new EventHandler(SetLetter)));
  mi.MenuItems.Add(2,new MenuItem("Set to C ", new EventHandler(SetLetter)));
  mi.MenuItems.Add(3,new MenuItem("Set to D", new EventHandler(SetLetter)));

private void SetLetter(object sender, System.EventArgs e)
{
  index = ????
}

C#, .net 2.0


You can cast the "sender" parameter (cast to MenuItem) to find the index in the mi.MenuItems collection.

Alternatively, I would set-up the event handlers using anonymous functions...

E.g.

mi.MenuItems.Add(0,new MenuItem("Set to A", (s, e) => SetLetter("A"));

Then you've got SetLetter as:

private void SetLetter(string letterToSetTo)
{
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜