-->

ContextMenuStrip.Owner属性空当检索套装在ToolStripMenuItemCo

2019-05-12 02:28发布

我有一个ContextMenuStrip设置有两个ToolStripItem秒。 第二ToolStripItem具有两个附加的嵌套ToolStripItem秒。 我将此定义为:

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表示我想回应。 这里有方法(我只列出了两个三种方法):

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
            }
        }
    }
}

这里是我的问题:

contextJumpTo_Click方法工作完全正常。 我们得到一路跌到哪里我确定DataGridView点击从哪里来,我可以继续进行。 该contextJumpTo ToolStripMenuItem然而,没有一个嵌套的菜单上的项目ContextMenuStrip

但我的方法contextJumpToHeatmapStart_Click不工作的权利。 当我坐下来,我确定线路owner.SourceControl中, SourceControl为空,我不能继续。 现在我知道,这ToolStripMenuItem下另一个在我嵌套ContextMenuStrip ,偏偏是SourceControl属性suddently空在我ContextMenuStrip

我如何获得SourceControl嵌套的ToolStripMenuItem一个ContextMenuStrip

Answer 1:

我认为这是一个错误。

我试图爬到工具条父母的名单才能到ContextStripMenu老板,这工作,但SourceControl属性总是空。

它看起来像常见的解决办法是设置在上下文菜单的打开控制:

private Control menuSource;

cms.Opening += cms_Opening;

void cms_Opening(object sender, CancelEventArgs e) {
  menuSource = ((ContextMenuStrip)sender).SourceControl;
}

那么你的代码基本上变成这样:

DataGridView dgv = menuSource as DataGridView;
if (dgv != null) {
  // do work
}


文章来源: ContextMenuStrip.Owner Property null When Retrieving From Nested ToolStripMenuItem