Context menu in VB.NET issue

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Guys,

I need to display a context menu (ContextMenuStrip) directly below the control that was clicked, e.g. a Button. I don't care about the exact mouse coordinates. How can I pop up a menu at the clicked control's lower-left corner?

thanks!
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
Is your issue that it's showing up in the wrong location? If you use an event like MouseUp (which you should since almost every app does it there instead of MouseDown), the coordinates should already be in screen coordinates. If they're not, you could try calling PointToScreen off the button and pass in the coordinates. I'm working off of memory here, but if the Show() method of the context menu takes a control *and* a point, the point will be relative to the control itself, so you'd have to call PointToClient on the points given to you.
 

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Originally posted by: Tencntraze
Is your issue that it's showing up in the wrong location? If you use an event like MouseUp (which you should since almost every app does it there instead of MouseDown), the coordinates should already be in screen coordinates. If they're not, you could try calling PointToScreen off the button and pass in the coordinates. I'm working off of memory here, but if the Show() method of the context menu takes a control *and* a point, the point will be relative to the control itself, so you'd have to call PointToClient on the points given to you.

But I don't care about the mouse coordinates. The menu must be shown in the same place (directly below the Button control), no matter where exactly the user clicked.....
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
ok, well then why not just take the Location + Height of the button as the mouse points and apply my previous statement with that?
 

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Originally posted by: Tencntraze
ok, well then why not just take the Location + Height of the button as the mouse points and apply my previous statement with that?

because then the menu shows up in some weird place outside of the form itself ;) these must be relative to the container (Frame), which own coordinates are relative to its container, so I'd have to do massive calcs to compute the screen coordinates :(
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
Now that I had a computer to test it, just try

Me.ContextMenuStrip1.Show(Me.Button1, New Point(0, Me.Button1.Height))

The point is relative to the specified control's location, so if you want it to show right below the button, you just provide a positive Y value as such.
 

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Originally posted by: Tencntraze
Now that I had a computer to test it, just try

Me.ContextMenuStrip1.Show(Me.Button1, New Point(0, Me.Button1.Height))

The point is relative to the specified control's location, so if you want it to show right below the button, you just provide a positive Y value as such.

brilliant, it works :) thank you sir!