C# = MenuItem.Click 处理程序 - 获取上下文菜单所属的对象?
这可能非常简单,但我没有看到它,因为这是漫长的一天的结束,如果是的话,我提前道歉。
我有一组按钮,右键单击时会弹出一个上下文菜单。该菜单有两个 MenuItem,它们都分配有 Click 处理函数。我在右键单击按钮时触发 ContextMenu 弹出,如下所示:
过度简化的示例:
public void InitiailizeButtonContextMenu()
{
buttonContextMenu = new ContextMenu();
MenuItem foo = new MenuItem("foo");
foo.Click += OnFooClicked;
MenuItemCollection collection = new MenuItemCollection(buttonContextMenu);
collection.Add(foo);
}
public void OnButtonMouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
// left click stuff handling
if (e.Button == MouseButtons.Right)
buttonContextMenu.Show((Button)sender, new Point(e.X, e.Y));
}
public void OnFooClicked(object sender, EventArgs e)
{
// Need to get the Button the ContextMenu .Show'd on in
// OnButtonMouseClick... thoughts?
}
ContextMenu buttonContextMenu;
我需要能够在 Click 处理程序中获取触发 ContextMenu 弹出的按钮对于 MenuItem,或者以某种方式获取它。 MenuItem.Click 采用 EventArgs,因此没有任何用处。我可以将对象发送者强制转换回 MenuItem,但我找不到任何可以告诉我是什么让它弹出的信息。这可能吗?
This might be incredibly simple and I'm not seeing it because it's the end of a long day, and if it is I apologize in advance.
I've got a set of Buttons that when right-clicked pop up a ContextMenu. The menu has two MenuItems, both of which have a Click handler function assigned. I'm triggering the ContextMenu to pop up on the right click of a button like so:
Overly simplified example:
public void InitiailizeButtonContextMenu()
{
buttonContextMenu = new ContextMenu();
MenuItem foo = new MenuItem("foo");
foo.Click += OnFooClicked;
MenuItemCollection collection = new MenuItemCollection(buttonContextMenu);
collection.Add(foo);
}
public void OnButtonMouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
// left click stuff handling
if (e.Button == MouseButtons.Right)
buttonContextMenu.Show((Button)sender, new Point(e.X, e.Y));
}
public void OnFooClicked(object sender, EventArgs e)
{
// Need to get the Button the ContextMenu .Show'd on in
// OnButtonMouseClick... thoughts?
}
ContextMenu buttonContextMenu;
I need to be able to get the Button that triggered the ContextMenu to pop up IN the Click handler for the MenuItem, or get it to it somehow. MenuItem.Click takes EventArgs, so nothing useful there. I can cast object sender back to MenuItem but I can't find anything that tells me what made it pop up. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 ContextMenu.SourceControl 属性,它为您提供对 Button 实例的引用。
Use the ContextMenu.SourceControl property, it gives you a reference to the Button instance back.
在上面的代码摘录中,当您调用
buttonContextMenu
的 show 方法时,当您右键单击按钮时,将按钮对象传递给buttonContextMenu
。要访问 OnFooClicked 方法中的按钮,只需将“发送者”转换回按钮即可。
*我不知道 MenuItem 是否是正确的演员表,但它应该遵循这些原则。
In the above code excerpt, when you call the show method of
buttonContextMenu
, you pass the button object tobuttonContextMenu
when you right-click on the button.To access the button in the OnFooClicked method simply cast the 'sender' back to a button.
*I don't know if MenuItem is the correct cast but it should be along those lines.
如果您使用 ContextMenuStrip 和 ToolStripItem,而不是 ContextMenu 和 MenuItem,您将需要:
使用常规 ContextMenu 和 MenuItem(来自 trycatch 对 Hans Passant 帖子的评论):
If you are using ContextMenuStrip and ToolStripItem, rather than ContextMenu and MenuItem, you'll need:
With a regular ContextMenu and MenuItem (from trycatch's comment on Hans Passant's post):
我并不是 100% 同意这一点——它来自我大约 2 年前编写的代码,但我希望它能让你继续前进。
I'm not 100% on this - its from code I wrote about 2 years ago, but I hope it gets you moving on this.