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.
Sam HobbsPosted Mar 3, 2010, 7:10 PM
theLizardPosted Mar 3, 2010, 3:01 PM
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 HobbsPosted Mar 3, 2010, 2:46 PM
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.
theLizardPosted Mar 3, 2010, 4:27 AM
theLizardPosted Mar 3, 2010, 4:21 AM
It works for me in test, I hope this helps.
Andy WebsterPosted Mar 3, 2010, 4:05 AM
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)
{
}
}
Andy WebsterPosted Mar 3, 2010, 3:34 AM
theLizardPosted Mar 2, 2010, 5:00 PM
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..