You can use the sender
paramter to get the ContextMenuStrip
then call the ContextMenuStrip.SourceControl
property to get the control that opened the context menu.
In this case you can try the following code.
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
var ctxStrip = sender as ContextMenuStrip;
if (ctxStrip == null)
return;
var rtb = ctxStrip.SourceControl as RichTextBox;
if (rtb == null)
return;
}
This code simply casts the sender
object to a ContextMenuStrip
if this is null
then return. (Although should never be). The next line captures the SourceControl
and casts the control to a RichTextBox
.
If the source control is not a RichTextBox
then the result will be null and we cancel as this shouldn't be null unless you bind the context menu to other controls aswell.