如何确定哪个控件被单击并且 contextMenuStrp 出现?

发布于 2025-01-08 03:58:37 字数 808 浏览 0 评论 0原文

我已将 contextMenuStrip 分配给两个 ListBox 控件的相同上下文菜单属性。 我想确定其中哪一个激活了上下文菜单。 因为我必须根据运行时单击的控件来更改一些条目。

        private void copyNotesToClipboardStripMenu_Click(object sender, EventArgs e)
        {
            ListBox cntrl = conMenuNotes.SourceControl as ListBox;
            //cntrl does not contain info about which ListBox was clicked :((

            //check which ListBox was clicked
            if (/*oneListBox*/)
            {
                 //do something                    
            }
            if(/*anotherLiskBox*/)
            {    
                //do something
            }              
        }

当我阅读另​​一篇文章时,它应该是 Label 属性中单击的控件的名称。

我看到 cntrl.Labelnull

我做错了什么? 请告诉我如何解决这种歧义。 谢谢你!

I've assigned a contextMenuStrip to the same contextual menu property of two ListBox controls.
I would like to determine which of them has activated the contextual menu.
Because I have to change some entries depending on which control was clicked at runtime.

        private void copyNotesToClipboardStripMenu_Click(object sender, EventArgs e)
        {
            ListBox cntrl = conMenuNotes.SourceControl as ListBox;
            //cntrl does not contain info about which ListBox was clicked :((

            //check which ListBox was clicked
            if (/*oneListBox*/)
            {
                 //do something                    
            }
            if(/*anotherLiskBox*/)
            {    
                //do something
            }              
        }

As i read another posts it should be the name of the clicked control in the Label property.

I see that cntrl.Label is null

What i'm doing wrong ?
Advice me how to resolve this ambiguity.
Thank you!

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

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

发布评论

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

评论(2

橘和柠 2025-01-15 03:58:37

不要将菜单分配给两个控件,而是使用控件上的右键单击事件来执行这样的函数,

  • 设置一个全局变量以指向刚刚单击的控件,
  • 鼠标坐标处显示您想要的菜单
  • 在您的 < 中现在的 code>copyNotesToClipboardStripMenu_Click 方法,您可以访问存储单击的控件的全局变量。

完成后也不要忘记将全局设置为 null 并确保使用使用时锁定

Instead of assigning the menu to the two controls, use the right click event on the controls to execute a function like this

  • set a global variable to point to the control that's just been clicked
  • display the menu you want at the mouse coordinates
  • now in your copyNotesToClipboardStripMenu_Click method you can access the global variable that's storing the clicked control

Also don't forget to set the global to null after you're done with it and to make sure you use lock when using it.

娇俏 2025-01-15 03:58:37

我发现一个对我来说效果很好的结果

        private void copyNotesToClipboardStripMenu_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            if (menuItem != null)
            {
                ContextMenuStrip calendarMenu = menuItem.Owner as ContextMenuStrip;
                if (calendarMenu != null)
                {
                    Control controlSelected = calendarMenu.SourceControl;
                }
            }
        }

controlSelected 对象的属性 Name 包含已激活上下文菜单的控件的名称。

I found a result that works fine for me

        private void copyNotesToClipboardStripMenu_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            if (menuItem != null)
            {
                ContextMenuStrip calendarMenu = menuItem.Owner as ContextMenuStrip;
                if (calendarMenu != null)
                {
                    Control controlSelected = calendarMenu.SourceControl;
                }
            }
        }

The property Name of controlSelected object contains the name of control which has activated the contextual menu.

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