确定哪些控制的ContextMenuStrip上使用 [英] Determine what control the ContextMenuStrip was used on
问题描述
我有一个的ContextMenuStrip
分配到几个不同的列表框。我试图找出的ContextMenuStrip
点击什么<$ C $ C>列表框,它是用于在时。我试过code以下作为一个开始,但它不能正常工作。该发件人
具有正确的价值,但是当我试图把它分配给 menuSubmitted
为null。
私人无效MenuViewDetails_Click(对象发件人,EventArgs的)
{
文本菜单menuSubmitted =发件人为文本菜单;
如果(menuSubmitted!= NULL)
{
控制sourceControl = menuSubmitted.SourceControl;
}
}
任何帮助将是巨大的。谢谢你。
使用下面的协助下,我想通了:
私人无效MenuViewDetails_Click(对象发件人,EventArgs的)
{
ToolStripMenuItem菜单项=发件人为ToolStripMenuItem;
如果(菜单项!= NULL)
{
的ContextMenuStrip calendarMenu = menuItem.Owner为的ContextMenuStrip;
如果(calendarMenu!= NULL)
{
控制controlSelected = calendarMenu.SourceControl;
}
}
}
有关文本菜单
:
的问题是,发件人
参数指向的项目的被点击,而不是上下文菜单本身在上下文菜单上。
这是一个简单的修补程序,但是,因为每个菜单项
暴露了一个<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.menu.getcontextmenu.aspx"><$c$c>GetContextMenu$c$c>方法,将告诉你哪些文本菜单
包含菜单项。
更改code以下内容:
私人无效MenuViewDetails_Click(对象发件人,EventArgs的)
{
//尝试转换发送到一个菜单项
菜单项菜单项=发件人的菜单项;
如果(菜单项!= NULL)
{
//检索包含该菜单项的文本菜单
文本菜单菜单= menuItem.GetContextMenu();
//获取,其中显示该上下文菜单控制
控制sourceControl = menu.SourceControl;
}
}
有关的ContextMenuStrip
:
如果你使用它改变的事情稍微的ContextMenuStrip
,而不是文本菜单
。这两个控制是不相关的彼此,和一个的实例不能铸造的其他的实例
与以前一样,项目的被点击了发件人
参数仍然是返回,所以你必须确定的ContextMenuStrip
拥有这个人的菜单项。你这样做,与<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.owner.aspx"><$c$c>Owner$c$c>物业。最后,您将使用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.sourcecontrol.aspx"><$c$c>SourceControl$c$c>物业,以确定哪些控制被显示的上下文菜单。
修改您的code像这样:
私人无效MenuViewDetails_Click(对象发件人,EventArgs的)
{
//尝试投发件人在ToolStripItem
ToolStripItem的菜单项=发件人为ToolStripItem的;
如果(菜单项!= NULL)
{
//获取拥有此ToolStripItem的的的ContextMenuStrip
老板的ContextMenuStrip = menuItem.Owner为的ContextMenuStrip;
如果(老板!= NULL)
{
//获取,其中显示该上下文菜单控制
控制sourceControl = owner.SourceControl;
}
}
}
I have a ContextMenuStrip
that is assigned to several different listboxes. I am trying to figure out when the ContextMenuStrip
is clicked what ListBox
it was used on. I tried the code below as a start but it is not working. The sender
has the correct value, but when I try to assign it to the menuSubmitted
it is null.
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ContextMenu menuSubmitted = sender as ContextMenu;
if (menuSubmitted != null)
{
Control sourceControl = menuSubmitted.SourceControl;
}
}
Any help would be great. Thanks.
Using the assistance below, I figured it out:
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
ContextMenuStrip calendarMenu = menuItem.Owner as ContextMenuStrip;
if (calendarMenu != null)
{
Control controlSelected = calendarMenu.SourceControl;
}
}
}
For a ContextMenu
:
The problem is that the sender
parameter points to the item on the context menu that was clicked, not the context menu itself.
It's a simple fix, though, because each MenuItem
exposes a GetContextMenu
method that will tell you which ContextMenu
contains that menu item.
Change your code to the following:
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;
}
}
For a ContextMenuStrip
:
It does change things slightly if you use a ContextMenuStrip
instead of a ContextMenu
. The two controls are not related to one another, and an instance of one cannot be casted to an instance of the other.
As before, the item that was clicked is still returned in the sender
parameter, so you will have to determine the ContextMenuStrip
that owns this individual menu item. You do that with the Owner
property. Finally, you'll use the SourceControl
property to determine which control is displaying the context menu.
Modify your code like so:
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;
}
}
}
这篇关于确定哪些控制的ContextMenuStrip上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!