问题描述
我有一个的ContextMenuStrip
设置有两个的ToolStripItem
秒。第二个的ToolStripItem
有两个额外的嵌套的ToolStripItem
秒。我把它称之为:
I have a ContextMenuStrip
setup with two ToolStripItem
s. The second ToolStripItem
has two additional nested ToolStripItem
s. I define this as:
ContextMenuStrip cms = new ContextMenuStrip();
ToolStripMenuItem contextJumpTo = new ToolStripMenuItem();
ToolStripMenuItem contextJumpToHeatmap = new ToolStripMenuItem();
ToolStripMenuItem contextJumpToHeatmapStart = new ToolStripMenuItem();
ToolStripMenuItem contextJumpToHeatmapLast = new ToolStripMenuItem();
cms.Items.AddRange(new ToolStripItem[] { contextJumpTo,
contextJumpToHeatmap});
cms.Size = new System.Drawing.Size(176, 148);
contextJumpTo.Size = new System.Drawing.Size(175, 22);
contextJumpTo.Text = "Jump To (No Heatmapping)";
contextJumpToHeatmap.Size = new System.Drawing.Size(175, 22);
contextJumpToHeatmap.Text = "Jump To (With Heatmapping)";
contextJumpToHeatmap.DropDownItems.AddRange(new ToolStripItem[] { contextJumpToHeatmapStart,
contextJumpToHeatmapLast });
contextJumpToHeatmapStart.Size = new System.Drawing.Size(165, 22);
contextJumpToHeatmapStart.Text = "From Start of File";
contextJumpToHeatmapLast.Size = new System.Drawing.Size(165, 22);
contextJumpToHeatmapLast.Text = "From Last Data Point";
我再设置一个事件侦听器为三 ToolStripMenuItem
s表示我想回应的单击事件。以下是方法(我只列出了两个三种方法):
I then setup an event listener for the click events of the three ToolStripMenuItem
s that I want to respond to. Here are the methods (I only listed two of the three methods):
void contextJumpTo_Click(object sender, EventArgs e)
{
// Try to cast the sender to a ToolStripItem
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
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
DataGridView dgv = owner.SourceControl as DataGridView;
if (dgv != null)
// DO WORK
}
}
}
void contextJumpToHeatmapStart_Click(object sender, EventArgs e)
{
// Try to cast the sender to a ToolStripItem
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
// Retrieve the ToolStripItem that owns this ToolStripItem
ToolStripMenuItem ownerItem = menuItem.OwnerItem as ToolStripMenuItem;
if (ownerItem != null)
{
// Retrieve the ContextMenuStrip that owns this ToolStripItem
ContextMenuStrip owner = ownerItem.Owner as ContextMenuStrip;
if (owner != null)
{
// Get the control that is displaying this context menu
DataGridView dgv = owner.SourceControl as DataGridView;
if (dgv != null)
// DO WORK
}
}
}
}
下面是这个问题,我有:
Here is the issue I have:
我的 contextJumpTo_Click
方法工作完全正常。我们得到了一路下跌的地方,我确定哪些的DataGridView
点击从哪里来的,我可以继续进行。该 contextJumpTo
ToolStripMenuItem
然而,而不是在一个嵌套的菜单项的ContextMenuStrip
。
My contextJumpTo_Click
method works perfectly fine. We get all the way down to where I determine which DataGridView
the click came from and I can proceed. The contextJumpTo
ToolStripMenuItem
is, however, NOT a nested menu item on the ContextMenuStrip
.
但我的方法 contextJumpToHeatmapStart_Click
不工作的权利。当我坐下来,我确定 owner.SourceControl
行, SourceControl
为空,我无法继续。现在我知道,这 ToolStripMenuItem
嵌套在一个又一个在我的的ContextMenuStrip
,但为什么是 SourceControl
财产suddently空我的的ContextMenuStrip
?
But my method for contextJumpToHeatmapStart_Click
does not work right. When I get down to the line where I determine owner.SourceControl
, the SourceControl
is null and I cannot proceed. Now I know that this ToolStripMenuItem
is nested under another one in my ContextMenuStrip
, but why is the SourceControl
property suddently null on my ContextMenuStrip
?
我如何获得 SourceControl
嵌套 ToolStripMenuItem
为的ContextMenuStrip
?
推荐答案
我认为这是一个错误。
我试图爬上工具条父母的名单去了ContextStripMenu所有者,它的工作,但SourceControl属性总是空。
I tried to crawl up the list of toolstrip parents to get to the ContextStripMenu owner, which worked, but the SourceControl property was always null.
它看起来像常见的解决办法是设置控件的上下文菜单的打开:
It looks like the common work around is to set the control on the opening of the context menu:
private Control menuSource;
cms.Opening += cms_Opening;
void cms_Opening(object sender, CancelEventArgs e) {
menuSource = ((ContextMenuStrip)sender).SourceControl;
}
那么你的code基本上变成这样:
Then your code basically turns into this:
DataGridView dgv = menuSource as DataGridView;
if (dgv != null) {
// do work
}
这篇关于ContextMenuStrip.Owner属性空当检索从嵌套ToolStripMenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!