1

I have some dynamically create labels with a context menu strip. When you right click the label it give you a context menu strip. How can I get their text when I click an item in their context menu strip? I know this is a really simple question but I need some help. I've tried

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

I'm trying to get the text of the label that generated the contextmenustrip.

y--
  • 588
  • 2
  • 10
  • 27

1 Answers1

2

I think you are looking for which control fired the context menu, something like this:

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

Converted to VB.Net from Determine what control the ContextMenuStrip was used on

Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225