C# – Find parent control of ToolStripMenuItem

cc#-4.0winforms

I have a ContextMenuStrip that I attach to several controls. It has the items { Add, Remove, Edit }. When a user right clicks on one of my listbox controls (which pops up this context menu) and selects 'Add', how can I derive the listbox control from the ToolStripMenuItem reference that is passed in?

    private void OnAddEntry(object sender, EventArgs e)
    {
        // Example: ?????
        ListBox lb = sender.Parent;
    }

Best Answer

Mark, try this:

((ContextMenuStrip)(((ToolStripMenuItem)sender).Owner)).SourceControl
Related Topic