I have a ContextMenuStrip
that is assigned to several different listboxes. I am trying to figure out when the ContextMenuStrip
is clicked what ListBox
it was used on. I tried the code below as a start but it is not working. The sender
has the correct value, but when I try to assign it to the menuSubmitted
it is null.
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ContextMenu menuSubmitted = sender as ContextMenu;
if (menuSubmitted != null)
{
Control sourceControl = menuSubmitted.SourceControl;
}
}
Any help would be great. Thanks.
Using the assistance below, I figured it out:
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
ContextMenuStrip calendarMenu = menuItem.Owner as ContextMenuStrip;
if (calendarMenu != null)
{
Control controlSelected = calendarMenu.SourceControl;
}
}
}