4

I am using a ContextMenuStrip in a WinForm project. I have added two item in it at design time- 1) Add Focus and 2) Remove Focus. In runtime I am adding few dropdown items in "Add Focus" item. Problem is, I am getting null when trying to retrieve source control name.

Here is my code:

ToolStripDropDownItem dropDownItem = sender as ToolStripDropDownItem;
ToolStripMenuItem mainItem = dropDownItem.OwnerItem as ToolStripMenuItem;
ContextMenuStrip contextMenuStrip = mainItem.Owner as ContextMenuStrip;
Control control =contextMenuStrip.SourceControl; //getting null here

What is the problem in this code? Am I missing something here ?

s.k.paul
  • 7,099
  • 28
  • 93
  • 168
  • possible duplicate of [ContextMenuStrip.Owner Property null When Retrieving From Nested ToolStripMenuItem](http://stackoverflow.com/questions/12094528/contextmenustrip-owner-property-null-when-retrieving-from-nested-toolstripmenuit) – LarsTech Nov 01 '12 at 14:26

1 Answers1

1

The context menu strip must be set as the ContextMenuStrip property of a user control or form :)

Source: https://www.codeproject.com/Questions/487557/SourceControlplusofplusContextMenuStripplusDropDow

Maritim
  • 2,111
  • 4
  • 29
  • 59