ContextMenuStrip.Owner Property null When Retrieving From Nested ToolStripMenuItem

前端 未结 1 1500
太阳男子
太阳男子 2020-12-01 19:00

I have a ContextMenuStrip setup with two ToolStripItems. The second ToolStripItem has two additional nested ToolStripItem

相关标签:
1条回答
  • 2020-12-01 19:19

    I believe that's a bug.

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

    Then your code basically turns into this:

    DataGridView dgv = menuSource as DataGridView;
    if (dgv != null) {
      // do work
    }
    
    0 讨论(0)
提交回复
热议问题