Question contextmenustrip accessing control?

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
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?
 
Hi,

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

VB.NET:
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
 
Back
Top