栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

确定使用了ContextMenuStrip的控件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

确定使用了ContextMenuStrip的控件

对于
ContextMenu

问题是该

sender
参数指向单击的上下文菜单上的 项目 ,而不是上下文菜单本身。

不过,这是一个简单的解决方法,因为每个方法都

MenuItem
公开了一个
GetContextMenu
方法,该方法将告诉您哪个
ContextMenu
包含该菜单项。

将您的代码更改为以下内容:

private void MenuViewDetails_Click(object sender, EventArgs e){    // Try to cast the sender to a MenuItem    MenuItem menuItem = sender as MenuItem;    if (menuItem != null)    {        // Retrieve the ContextMenu that contains this MenuItem        ContextMenu menu = menuItem.GetContextMenu();        // Get the control that is displaying this context menu        Control sourceControl = menu.SourceControl;    }}

对于
ContextMenuStrip

如果使用a

ContextMenuStrip
而不是a,它的确会稍有改变
ContextMenu
。这两个控件彼此不相关,并且一个实例不能转换为另一个实例。

与以前一样,被单击的 项目
仍会在

sender
参数中返回,因此您将必须确定
ContextMenuStrip
拥有此单独菜单项的。您可以通过该
Owner
属性来实现。最后,您将使用该
SourceControl
属性来确定哪个控件正在显示上下文菜单。

像这样修改您的代码:

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 menuControl sourceControl = owner.SourceControl;        }     } }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号