Find Parent ToolStripMenuItem Control

I have a ContextMenuStrip that I attach to multiple controls. It has elements {Add, Remove, Edit}. When a user right-clicks on one of my list controls (which appears in this context menu) and selects Add, how can I get the list control from the ToolStripMenuItem link that is passed to?

private void OnAddEntry(object sender, EventArgs e) { // Example: ????? ListBox lb = sender.Parent; } 
+6
source share
2 answers

Note, try the following:

 ((ContextMenuStrip)(((ToolStripMenuItem)sender).Owner)).SourceControl 
+15
source

I assume that you can climb the chain of parents until you find the list.

You can speed this up by using the OwnerItem property to go directly to the toolbar.

You can always set an element tag to a list, and then just use it as needed.

+1
source

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


All Articles