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

发布于 2024-07-24 09:51:59 字数 213 浏览 7 评论 0原文

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

我有两个标签,lblOn 和 lblOff。 我将“一个”上下文菜单链接到两个标签,以放弃必须制作两个相同的标签。

我将如何继续找出名为 contextmenu.menuitem 的标签对象? 这样,单击的菜单项就知道它的上下文菜单是由 lblOn 标签还是 lblOffline 调用的?

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

I have two labels, lblOn and 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? That way the clicked on menuitem knows if it was it it's contextmenu was called by the lblOn label or lblOffline?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拒绝两难 2024-07-31 09:51:59

检查ContextMenuStripSourceControl 属性。

Check the SourceControl property of the ContextMenuStrip.

丢了幸福的猪 2024-07-31 09:51:59

漠视。 经过更多谷歌搜索后,我找到了一个解决方案+代码示例。

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

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

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

高速公鹿 2024-07-31 09:51:59

我知道这是很多个月前的问题,但我还没有真正找到
一个简单的答案与代码...
我知道 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;
        }
    }

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...

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;
        }
    }
贪了杯 2024-07-31 09:51:59

用这个:

contextMenuStrip1.SourceControl;

Use this:

contextMenuStrip1.SourceControl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文