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.

theLizardRank #1,079 · 1.8K reputationPosted Mar 2, 2010, 12:00 PM EST
What are you trying to do? Is it possible you may be going about it the wrong way?
These are my thoughts, you want to drop a context menu from a control, any control and show a value related to that control, is this about right?
If so then you do not need to know who sender is in respect to the way you are going about it now but you do need to know who wants / needs to show the menu.
In the MouseClick event (the same MouseClick event can be assigned to multiple objects)
private void GENERIC_MouseClick(object sender, MouseEventArgs e)
{
switch(sender.getType().Name)
{
case "Label":
//cast to the sender control type
mnuLabel = (Label)sender;
mnuLabelText = mnuLabel.Text;
}
}
This is assuming I understand your Q correctly..
Andy Webster115 reputationPosted Mar 2, 2010, 10:34 PM EST
Andy Webster115 reputationPosted Mar 2, 2010, 11:05 PM EST
private void copyTextToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
#region Try
ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
ToolStrip menu = menuItem.GetCurrentParent();
ContextMenuStrip menuStrip = (ContextMenuStrip)menu;
Label lbl = (Label)(menuStrip.SourceControl);
SetClipboardText(lbl.Text);
#endregion Try
}
catch (Exception)
{
}
}
theLizardRank #1,079 · 1.8K reputationPosted Mar 2, 2010, 11:21 PM EST
It works for me in test, I hope this helps.
theLizardRank #1,079 · 1.8K reputationPosted Mar 2, 2010, 11:27 PM EST
Sam HobbsRank #107 · 20.5K reputationPosted Mar 3, 2010, 9:46 AM EST
You say you "want to make the context menu I am using to be generic to all label controls" but you also say "I am trying to get a reference to the label control from the ToolStripMenuItem click event"; that seems inconsistent to me.
If I understand what you are trying to do, then the easiest way would be to add a ContextMenu to the label(s) when the form is created. I understand that you don't want to "iterate through all the controls on the form" but I am suggesting going the otehr direction; just process the labels in the ToolStripMenuItem or whatever.
theLizardRank #1,079 · 1.8K reputationPosted Mar 3, 2010, 10:01 AM EST
he is not doing anything that is inconsistent and even if he where, he has a reason for doing things the way he wants to.
" the easiest way would be to add a ContextMenu to the label(s) when the form is created" He already does that Sam.
If the code he has used as his solution works then that is fine, if it can be done better then he will find a way that is EVOLUTION.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 3, 2010, 2:10 PM EST