ContextMenuStrip.Owner Property null When Retrieving From Nested ToolStripMenuItem

对着背影说爱祢 提交于 2019-11-27 09:05:18

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!