c#判断右键菜单(ContextMenuStrip)是从哪个控件弹出来的方法
1.方法一:在contextMenuStrip1打开时获取控件名称
双击contextMenuStrip1在它的opening事件中写入下面的代码:
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
string whichcontrol_name= (sender as ContextMenuStrip).SourceControl.Name;
MessageBox.Show(whichcontrol_name);
}
————————————————
2.方法二:目前不是很懂
private void MenuViewDetails_Click(object sender, EventArgs e)
{
// Try to cast the sender to a ToolStripItem
ToolStripItem menuItem = sender as ToolStripItem;
if (menuItem != null)
{
// Retrieve the ContextMenuStrip that owns this ToolStripItem
ContextMenuStrip owner = menuItem.Owner as ContextMenuStrip;
if (owner != null)
{
// Get the control that is displaying this context menu
Control sourceControl = owner.SourceControl;
}
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u014683488/article/details/106189658
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步