I have set of dynamically created checkboxes on a panel and also have implemented ContextMenuStrip on all checkboxes. I am unable to detect that which control currently displays the shortcut menu defined in the ContextMenuStrip.
Asked
Active
Viewed 230 times
-6
-
2Please add some code which clear ur problem. Which also show what you have tried before. – Ankush Madankar Dec 16 '14 at 05:49
-
Are you really using a ContextMenu?...or a ContextMenuStrip? – Idle_Mind Dec 16 '14 at 06:39
2 Answers
1
I have got the answer.
private void MenuViewDetails_Click(object sender, EventArgs e)
{
// Try to cast the sender to a MenuItem
MenuItem menuItem = sender as MenuItem;
if (menuItem != null)
{
// Retrieve the ContextMenu that contains this MenuItem
ContextMenu menu = menuItem.GetContextMenu();
// Get the control that is displaying this context menu
Control sourceControl = menu.SourceControl;
}
}

Vipin Sharma
- 594
- 5
- 19
0
Use the SourceControl() property.
With a ContextMenu:
private void menuItem1_Click(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)contextMenu1.SourceControl;
Console.WriteLine(cb.Name);
}
With a ContextMenuStrip:
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)contextMenuStrip1.SourceControl;
Console.WriteLine(cb.Name);
}

Idle_Mind
- 38,363
- 3
- 29
- 40