10

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

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我在这里看不到的属性。

我正在使用 Visual Studio 2008、VB.Net winforms。

4

5 回答 5

23
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
于 2010-01-15T21:43:46.507 回答
6

您的发件人是 ToolStripMenuItem - 投射它。
它的所有者是一个 ContextMenuStrip——得到它。

SourceControl 是 ContextMenuStrip 上的一个属性,它引用显示 ContextMenuStrip 的最后一个控件。

于 2010-01-15T21:36:37.543 回答
0
Private Sub kdgToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles kdgToolStripMenuItem.Click
    Dim sms = (sender.GetCurrentParent()).SourceControl.name
    MsgBox(sms)
End Sub

'///快点

于 2014-10-10T08:26:40.520 回答
0
Private Sub cmsRightClick_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cmsRightClick.MouseClick
    Dim s As String = CType(sender, ContextMenuStrip).GetItemAt(CType(sender, ContextMenuStrip).DisplayRectangle.X, _
     CType(sender, ContextMenuStrip).DisplayRectangle.Y + e.Y).Text.Trim()


    MsgBox(s)
    Select Case s 
        Case Is = "Select Summary Total"
            Dim x = 0
        Case Is = "Select Collections"
            Dim x = 1
        Case Is = "UnSelect"
            Dim x = 2
        Case Is = "Reconcile"
            Dim x = 3
        Case Is = "Undo Reconciliation"
            Dim x = 4
    End Select
End Sub
于 2017-07-28T03:27:34.000 回答
-1

在 VB.NET 2013 上,这项工作非常好:

Dim cms As ContextMenuStrip = CType(sender, ContextMenuStrip)
MessageBox.Show(cms.SourceControl.Name)
于 2015-02-10T19:47:11.420 回答