您如何获得单击以打开 ContextMenuStrip 的控件? [英] How do you get the control that was clicked to open a ContextMenuStrip?

查看:22
本文介绍了您如何获得单击以打开 ContextMenuStrip 的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 ContextMenuStrip 用于多个控件,并且我正在尝试找出获得实际单击以打开上下文菜单的控件的最佳方法.发件人只提供了 ToolStripMenuItem 引用,它有一个引用 ContextMenuStrip 的 Owner 属性,但我不知道如何判断点击来自哪个控件.一定有一个简单的方法来检查这个,对吧?我正在 ToolStripMenuItem 的点击事件中检查它.

I'm using a ContextMenuStrip for multiple controls and I'm trying to figure out the best way to get the control that was actually clicked on to open the Context Menu. The sender just gives the ToolStripMenuItem reference, which has an Owner property that references the ContextMenuStrip, but I cannot figure out how to tell which control the click came from. There must be a simple way to check this, right? I'm checking it in the ToolStripMenuItem's click event.

Friend WithEvents mnuWebCopy As System.Windows.Forms.ToolStripMenuItem
...
Private Sub mnuWebCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuWebCopy.Click

我发现了一个类似的帖子 关于这一点,但提到使用 SourceControl 属性,我在这里没有看到.

I found a similar post about this, but that mentions using a SourceControl property which I do not see on here.

我使用的是 Visual Studio 2008、VB.Net winforms.

I'm using Visual Studio 2008, VB.Net winforms.

推荐答案

Private Sub mnuWebCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuWebCopy.Click

Dim myItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(myItem.Owner, ContextMenuStrip)

MessageBox.Show(cms.SourceControl.Name)

End Sub

这篇关于您如何获得单击以打开 ContextMenuStrip 的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆