Andy Webster

Andy Webster

  • NA
  • 117
  • 0

Reference to a window control from context menu item click event handler

Mar 2 2010 5:31 AM
Can anyone tell me how I can get a reference to the host control of a context menu from within the click event handler for a ToolStripMenuItem.

I am trying to create a generic context menu for a label control that allows the user to copy the label text to the clipboard. My current code is specific to the label:

        private void copyTextToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                SetClipboardText(myLabel.Text);
            }
            catch (Exception ex)
            {
            }
        }

The sender is a ToolStripMenuItem so I can get a reference to it by casting the sender. How can I get a reference to the control, that is the label,  which the menu was dropped down from?



        private void copyTextToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
                Label myLabel = // WHAT GOES HERE ?
                SetClipboardText(myLabel.Text);
            }
            catch (Exception ex)
            {
            }
        }

Thanks for any help you can give.



Answers (8)