Prevent contextmenustrip from opening

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
481
Programming Experience
3-5
I have a form and i set a contextmenustrip to it. Then i added a picturebox to the form. Can i make it so that if i right click the picturebox the form's contextmenustip doesnt open? Can i just prevent it from opening instead of giving it another contextmenustrip?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,250
Location
Sydney, Australia
Programming Experience
10+
I have never been in a position where it's an issue but it appears that child controls inherit their parent's ContextMenuStrip implicitly unless you you explicitly set it otherwise to something other than Nothing. It seems like a bit of a hack but this is the best method I can come up with to do what you want:
Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
    e.Cancel = Me.RectangleToScreen(Me.PictureBox1.Bounds).Contains(Control.MousePosition)
End Sub
 

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
481
Programming Experience
3-5
It seems to better just assign it an empty cms that does nothing.
 
Top Bottom