ContextMenuStrip.Owner Property null When Retrieving From Nested ToolStripMenuItem

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
}

Leave a Comment