Search code examples
c#winformscontextmenustrip

Rename a checkbox by implementing ContextMenu c# winforms


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.


Solution

  • 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;    
        }
    }