ContextMenuStrip positioning

andyh20

Member
Joined
Aug 4, 2009
Messages
11
Programming Experience
5-10
I'm launching a contextmenustrip using its show method but am struggling to show it at the current mouse position.

if I use MousePosition in the call to Show it shows nowhere near. Instead I've been writing the x and y coordiantes of the mouse to module variables in the form's mouse down event, and then setting the position of the contextmenustrip to them.

This nearly works but is putting the centre of the menu in the x,y I give it. How do i get the menu strip to put the left corner at the provided x,y (or adjust x,y to have the same effect?

Thanks
Andrew
 
When you call Show you are supposed to specify a control that you're showing the menu on and then the location is relative to that control, e.g.
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click
    Me.ContextMenuStrip1.Show(Me.Button1, _
                              Me.Button1.PointToClient(Windows.Forms.Cursor.Position))
End Sub
 
I just realised that you asked about the form's MouseDown event. That's even easier because the MouseDown already gives you the position of the mouse pointer relative the to control raising the event:
VB.NET:
Private Sub Form1_MouseDown(ByVal sender As Object, _
                            ByVal e As MouseEventArgs) Handles Me.MouseDown
    Me.ContextMenuStrip1.Show(Me, e.Location)
End Sub
 
If you want the default behaviour why not just select the menu strip in the forms/controls ContextMenuStrip property in Designer ? No code needed for this.
 
If you want the default behaviour why not just select the menu strip in the forms/controls ContextMenuStrip property in Designer ? No code needed for this.

I was assuming it was for a left click rather than a right click, but maybe not.
 
Back
Top