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

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

问题描述

我正在为多个控件使用一个 ContextMenuStrip ,我试图找出实际点击的控件打开上下文菜单的最佳方法。发件人只给出了 ToolStripMenuItem 引用,它具有引用 ContextMenuStrip 的Owner属性,但是我无法弄清楚告诉点击来自哪个控件。必须有一个简单的方法来检查这个,对吧?我正在检查 ToolStripMenuItem 的点击事件。

  Friend WithEvents mnuWebCopy As System.Windows.Forms.ToolStripMenuItem 
...
私有子mnuWebCopy_Click(ByVal sender As Object,ByVal e As System.EventArgs)处理mnuWebCopy.Click

我发现一个类似的帖子,但是提到使用我在这里看不到的 SourceControl 属性。



我使用Visual Studio 2008,VB.Net winforms。

解决方案

私有子mnuWebCopy_Click(ByVal sender As Object,ByVal e As System.EventArgs)处理mnuWebCopy.Click

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

MessageBox.Show(cms.SourceControl.Name)

End Sub


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

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

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天全站免登陆