简体   繁体   English

C#:当链接到两个不同的对象时,如何检测谁是上下文菜单菜单项的调用者?

[英]C#: How to detect who is the caller of a context menu's menu item when linked to two different objects?

C#: How to detect who is the caller of a context menu's menu item when linked to two different objects? C#:当链接到两个不同的对象时,如何检测谁是上下文菜单菜单项的调用者?

I have two labels, lblOn and lblOff. 我有两个标签,lblOn和lblOff。 I am linking 'one' contextmenu to both labels to discard having to make two of the same. 我将“一个”上下文菜单链接到两个标签,以丢弃必须制作两个相同的标签。

How would I go upon finding out which label object called the contextmenu.menuitem? 我将如何找出名为contextmenu.menuitem的标签对象? That way the clicked on menuitem knows if it was it it's contextmenu was called by the lblOn label or lblOffline? 这样点击菜单项就知道是不是它的上下文菜单是由lblOn标签还是lblOffline调用的?

检查ContextMenuStripSourceControl属性。

Disregard. 漠视。 After googling a bit more, I found a solution + code example. 谷歌搜索后,我找到了解决方案+代码示例。

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);
                }
            }
        }
    }
}

Source: http://www.tek-tips.com/viewthread.cfm?qid=1441041&page=8 资料来源: http//www.tek-tips.com/viewthread.cfm?qid = 1441041& page = 8

I know this is a question from many moons ago but I havent really been able to find a simple answer with code... I know SLaks kind of pointed it out, but I think others out there need a code sample... 我知道这是一个很多月前的问题,但我还没有真正找到一个简单的代码答案......我知道SLaks有点指出它,但我认为其他人需要一个代码示例......

I wanted to know who the called the context menu between either a rich text box or a label. 我想知道在富文本框或标签之间调用上下文菜单的人。 The reason is I wanted only one context menu and wanted the copy button within it to be disabled if the caller was the rich text box with nothing selected. 原因是我只想要一个上下文菜单,并且如果调用者是没有选择任何内容的富文本框,则希望禁用其中的复制按钮。

Heres my code: 继承我的代码:

    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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM