gpt4 book ai didi

C# = MenuItem.Click 处理程序 - 获取上下文菜单所属的对象?

转载 作者:行者123 更新时间:2023-11-30 13:41:10 25 4
gpt4 key购买 nike

这可能非常简单,我没有看到它,因为这是漫长的一天的结束,如果是我提前道歉。

我有一组按钮,当右键单击时会弹出一个上下文菜单。该菜单有两个 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;

我需要能够让触发 ContextMenu 的按钮在 MenuItem 的 Click 处理程序中弹出,或者以某种方式获取它。 MenuItem.Click 采用 EventArgs,因此没有任何用处。我可以将 object sender 转换回 MenuItem 但我找不到任何可以告诉我是什么让它弹出的信息。这可能吗?

最佳答案

使用 ContextMenu.SourceControl 属性,它会返回对 Button 实例的引用。

关于C# = MenuItem.Click 处理程序 - 获取上下文菜单所属的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5628201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com