context menu parent?

Hi I added a context menu on label (c#, winforms). my context menu having 3 child items and i want to display label text when i click on any one of context menu items.

thanks in advance

The ContextMenuStrip control has a SourceControl property, that will have a reference to the control that opened it. You can use that to extract the text from the control:

private void MenuStripItem_Click(object sender, EventArgs e)
{
    ToolStripItem item = (sender as ToolStripItem);
    if (item != null)
    {
        ContextMenuStrip owner = item.Owner as ContextMenuStrip;
        if (owner != null)
        {
            MessageBox.Show(owner.SourceControl.Text);
        }
    }
}

If you instead of a ContextMenuStrip use a ContextMenu, the code should look like this:

private void menuItem1_Click(object sender, EventArgs e)
{
    MenuItem item = (sender as MenuItem);
    if (item != null)
    {
        ContextMenu owner = item.Parent as ContextMenu;
        if (owner != null)
        {
            MessageBox.Show(owner.SourceControl.Text);
        }
    }
}

Determine what control the ContextMenuStrip was used on, For a ContextMenu : The problem is that the sender parameter points to the item on the context menu that was clicked, not the context menu  If you assign a ContextMenu to the FrameworkElement.ContextMenu or FrameworkContentElement.ContextMenu property, the ContextMenuService class handles context menu operations in response to user interaction. Part of the work the ContextMenuService does is opening the ContextMenu when the user clicks the right mouse button over the control.

Its the best in one line:

Control control = ((ContextMenuStrip)((ToolStripItem)sender).Owner).SourceControl;

Getting the parent object from a context menu - C# / C Sharp, one menu item called delete. All context menus share the same click event. I want to be able to determine which button it has come from (get the You can determine this from the SourceSource control property of the Context menu during the opening event. This is the VB.net code, haven't checked for the C# equiv. Private Sub ContextMenuStrip1_Opening( ByVal sender As Object , ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening Debug.WriteLine

Get Context Menu Parent Control Name MessageBox.Show(contextMenuStrip1.SourceControl.Name.ToString());

ContextMenu.SourceControl Property (System.Windows.Forms , You can determine this from the SourceSource control property of the Context menu during the opening event. This is the VB.net code, haven't  Use the uicontextmenu function to create a context menu and set properties. By changing property values, you can modify the appearance and behavior of a context menu. Use dot notation to refer to a specific context menu object and property. You can parent a context menu to a figure created with either the uifigure or figure function.

ContextMenu Class (System.Windows.Controls), contextMenus API to add items to Google Chrome's context menu. is visible at once, Google Chrome automatically collapses them into a single parent menu. Use the Parent property to determine the parent menu item or to access its properties.

[Solved] How to find the Parent Control of a ContextMenuStrip , You can add both parent and children items to the viewer context menu. When a user selects a parent menu item, it displays any child items that you defined. Context Menus in WPF. Context Menus can be defined on any WPF controls by setting the ContextMenu property to an instance of a ContextMenu.The items of a context menu are normal MenuItems.

chrome.contextMenus, The context menu is invisable until triggered by a user action on the parent object​. In this example, the context menu is displayed when one left-clicks on the  The implementations differ: Microsoft Word was one of the first applications to only show sub-entries of some menu entries after clicking an arrow icon on the context menu, otherwise executing an action associated with the parent entry. This makes it possible to quickly repeat an action with the parameters of the previous execution, and to better separate options from actions.

Comments
  • where do want the text and where from? From the label or on the label?
  • Have you considered using a combo box? This might be more intuitive for your users to understand
  • i want to display the label text (label is nothing but paret for context menu)
  • As fredrik says use SourceControl. Can you use ((Label)SourceControl).Text
  • i want to display the label text (label is nothing but paret for context menu)
  • hey here we are getting MenuItem not ToolStripItem