C#:如何检测上下文菜单的调用者';当链接到两个不同的对象时,是否显示菜单项?
C#:当链接到两个不同的对象时,如何检测上下文菜单菜单项的调用者 我有两个标签,lblOn和lblOff。我将'one'contextmenu链接到两个标签,以避免制作两个相同的标签C#:如何检测上下文菜单的调用者';当链接到两个不同的对象时,是否显示菜单项?,c#,.net,winforms,contextmenu,menuitem,C#,.net,Winforms,Contextmenu,Menuitem,C#:当链接到两个不同的对象时,如何检测上下文菜单菜单项的调用者 我有两个标签,lblOn和lblOff。我将'one'contextmenu链接到两个标签,以避免制作两个相同的标签 我如何才能找到名为contextmenu.menuitem的标签对象?这样,点击菜单项就知道它的上下文菜单是由lblOn标签还是lblOffline调用的?检查ContextMenuStrip的SourceControl属性。在谷歌搜索了一段时间后,我发现了一个解决方案+代码示例 private void dele
我如何才能找到名为contextmenu.menuitem的标签对象?这样,点击菜单项就知道它的上下文菜单是由lblOn标签还是lblOffline调用的?检查
ContextMenuStrip
的SourceControl
属性。在谷歌搜索了一段时间后,我发现了一个解决方案+代码示例
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
//Make sure the sender is a ToolStripMenuItem
ToolStripMenuItem myItem = sender as ToolStripMenuItem;
if (myItem != null)
{
//Get the ContextMenuString (owner of the ToolsStripMenuItem)
ContextMenuStrip theStrip = myItem.Owner as ContextMenuStrip;
if (theStrip != null)
{
//The SourceControl is the control that opened the contextmenustrip.
//In my case it could be a linkLabel
LinkLabel linkLabel = theStrip.SourceControl as LinkLabel;
if (linkLabel == null)
MessageBox.Show("Invalid item selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
if (MessageBox.Show(string.Format("Are you sure you want to remove BOL {0} from this Job?", linkLabel.Text), "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
linkLabel.Text = Program.NullValue(linkLabel);
}
}
}
}
}
资料来源:
我知道这是许多卫星以前提出的问题,但我还没有真正找到答案 一个简单的答案与代码。。。 我知道SLaks指出了这一点,但我认为其他人需要一个代码示例 我想知道是谁调用了富文本框或标签之间的上下文菜单。 原因是我只需要一个上下文菜单,并希望其中的复制按钮是可用的 如果调用方是未选择任何内容的富文本框,则禁用 这是我的密码:
private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
//get the context menu (it holds the caller)
ContextMenuStrip contextMenu = sender as ContextMenuStrip;
//get the callers name for testing
string controlName = contextMenu.SourceControl.Name;
//test if it is infact me rich text editor making the call.
if (controlName == "text_rchtxt")
{
//if I have nothing selected... I should not be able to copy
if (text_rchtxt.SelectedText == "")
copy_shrtct.Enabled = false;
}
else
{
//if I do have something selected or if its another control making the call, enable copying
copy_shrtct.Enabled = true;
}
}
使用以下命令:
contextMenuStrip1.SourceControl;
如果源是datagridview,我如何知道是哪一行还是哪一个单元格?