右键单击时动态创建项目的文本

时间:2013-02-27 17:21:03

标签: vb.net

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

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

我正在尝试获取生成contextmenustrip的标签文本。

1 个答案:

答案 0 :(得分: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

Determine what control the ContextMenuStrip was used on

转换为VB.Net