SourceControl of ContextMenuStrip DropDownItem returns null

I am using ContextMenuStrip in a WinForm project. I added two elements at this time of development: 1) Add focus and 2) Remove focus. At runtime, I add a few drop-down elements to the Add Focus element. The problem is that I get null when trying to get the name of the control source.

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? Did I miss something?

+4
source share
1 answer

The context menu bar should be set as the ContextMenuStrip property for the user control or form :)

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

0
source

Source: https://habr.com/ru/post/1443450/


All Articles