1

我有一些带有上下文菜单条的动态创建标签。当您右键单击标签时,它会给您一个上下文菜单条。当我单击上下文菜单条中的项目时,如何获取他们的文本?我知道这是一个非常简单的问题,但我需要一些帮助。我试过了

Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        MsgBox(sender.Text)
End Sub

我正在尝试获取生成上下文菜单条的标签文本。

4

1 回答 1

2

我认为您正在寻找哪个控件触发了上下文菜单,如下所示:

Private Sub DeleteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DeleteToolStripMenuItem.Click

  Dim menuItem As ToolStripItem = TryCast(sender, ToolStripItem)
  If menuItem IsNot Nothing Then
    Dim owner As ContextMenuStrip = TryCast(menuItem.Owner, ContextMenuStrip)
    If owner IsNot Nothing Then
      Dim sourceControl As Control = owner.SourceControl
      MessageBox.Show(sourceControl.Text)
    End If
  End If
End Sub

从确定使用 ContextMenuStrip 的控件转换为 VB.Net

于 2013-02-27T17:43:56.487 回答