I'm trying to grab the control that generated the context menu, as there will be several listview's using the same context menu.
I've done this before, but it appears to have gotten 1000x more complicated now I'm using an embedded combobox in the context menu:
When I select an item in the combo box, I need to determine which listview spawned the menu:
private void tsCboAddCharList_SelectedIndexChanged(object sender, EventArgs e) {
if (tsCboAddCharList.SelectedItem == null) return;
ContextMenuStrip theTSOwner;
if (sender.GetType().Name.Contains("ToolStripComboBox")) {
ToolStripComboBox theControl = sender as ToolStripComboBox;
ToolStripDropDownMenu theMenu = theControl.Owner as ToolStripDropDownMenu;
ContextMenuStrip theTlStrip = theMenu.OwnerItem.Owner as ContextMenuStrip;
ContextMenuStrip theCtxStrip = theTlStrip as ContextMenuStrip;
theTSOwner = theCtxStrip;
} else {
theTSOwner = (ContextMenuStrip)((ToolStripItem)sender).Owner;
}
ListView callingListV = (ListView)theTSOwner.SourceControl; //always null
What am I doing wrong?
Try this code:
If you are completely sure that this event handler can be called only for this particular menu item, you could omit the checks (but I'd not recommend this):