Question contextmenustrip accessing control?

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
481
Programming Experience
3-5
I have dynamically created a picturebox that linked to a dynamically created contextmenustrip. On the contextmenustrip.click, can i get the info from the picturebox.tag?
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

Yes, you can access the SourceControl of the ContextMenuStrip to find out the control that initiated the sub menu. Try this:-

Code:
Public Class Form1
  Private myCMS As New ContextMenuStrip
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim myMI As ToolStripItem = myCMS.Items.Add("MyOption")
    AddHandler myMI.Click, AddressOf MyMenuItemClick
 
    PictureBox1.ContextMenuStrip = myCMS
  End Sub
 
  Private Sub MyMenuItemClick(sender As System.Object, e As System.EventArgs)
    MsgBox(CType(sender, ToolStripItem).Text)
    MsgBox(myCMS.SourceControl.Tag)
  End Sub
End Class
Cheers,

Ian
 
Top Bottom