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;
}
Mark, try this:
((ContextMenuStrip)(((ToolStripMenuItem)sender).Owner)).SourceControl
I'm guessing you can go up the chain of parents until you find the listbox.
You may be able to speed this up using the OwnerItem property to get straight to the toolstrip.
You could always set the item's Tag to the listbox and then just use it as require.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With